2012-11-02 73 views
14

我有一個自定義的UISlider,對於大手指的人來說,由於「拇指圖像」的大小抓住並滑動相對困難。有沒有辦法在不改變圖像大小的情況下增加可點擊/可拖動區域的大小?自定義UISlider - 增加「熱點」的大小

下面的代碼我有創建自定義滑塊是否有幫助:

[slider setMaximumTrackImage:[[UIImage imageNamed:@"max.png"] 
              resizableImageWithCapInsets:UIEdgeInsetsMake(0, 20, 0, 20)] 
            forState:UIControlStateNormal]; 
[slider setMinimumTrackImage:[[UIImage imageNamed:@"min.png"] 
              resizableImageWithCapInsets:UIEdgeInsetsMake(0, 20, 0, 20)] 
            forState:UIControlStateNormal]; 
[slider setThumbImage:[UIImage imageNamed:@"thumb.png"] 
          forState:UIControlStateNormal]; 
+2

你可以作弊 - 在你的拇指圖像周圍添加一個透明像素的邊框 - 這將增加它的大小,但它看起來不同:) – deanWombourne

+0

如果我這樣做了,我將無法將滑塊設置爲0或1.(像它將被設置爲零,但它看起來不像是由於左側/右側的透明邊框的形象。) – unsunghero

+0

所以在軌道圖像的左側/右側添加填充。然後,補償你的代碼中不能達到0或1。 – deanWombourne

回答

24

我結束了繼承的UISlider和重寫此方法:

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event { 
    CGRect bounds = self.bounds; 
    bounds = CGRectInset(bounds, -10, -15); 
    return CGRectContainsPoint(bounds, point); 
} 

這擴展了在左側和右側10個像素,在頂部和底部的15個像素的觸摸區域。

+4

這似乎不適用於iOS 6+ – elsurudo

+0

這似乎在iOS7上運行良好。我想加厚軌道的邊界,所以我分類了UISlider並重新實現(CGRect)trackRectForBounds:(CGRect)邊界,但可點擊區域非常小。以上解決了這個問題 – brendan

+1

這似乎沒有在iOS6上生效,但在iOS7上可用。有關iOS 6支持的任何建議? – RealCasually

-1

調整大小框架和改變圖像對齊到所需的一個。您可以使其對齊居中,左側,右側,頂部或底部。這樣做:

CGRect newFrame = sliderHandle.frame; 
CGRectSetWidth(newFrame, 80); 
CGRectSetHeight(newFrame, 80); 
[sliderHandle setFrame:newFrame]; 
[sliderHandle setContentMode:UIViewContentModeCenter]; 

所以你的觸摸幀會更大,圖像的比例將保持不變。

+0

這只是調整滑塊的可見高度和寬度,並不會增加觸摸區域。 – unsunghero

+0

您也可以調整實際滑塊的視圖,這會收集觸摸 –

7

該解決方案可在iOS 8:

class ExtUISlider: UISlider { 

    var thumbTouchSize : CGSize = CGSizeMake(50, 50) 

    override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool { 
     let bounds = CGRectInset(self.bounds, -thumbTouchSize.width, -thumbTouchSize.height); 
     return CGRectContainsPoint(bounds, point); 
    } 

    override func beginTrackingWithTouch(touch: UITouch, withEvent event: UIEvent) -> Bool { 
     let thumbPercent = (value - minimumValue)/(maximumValue - minimumValue) 
     let thumbSize = thumbImageForState(UIControlState.Normal)!.size.height 
     let thumbPos = CGFloat(thumbSize) + (CGFloat(thumbPercent) * (CGFloat(bounds.size.width) - (2 * CGFloat(thumbSize)))) 
     let touchPoint = touch.locationInView(self) 

     return (touchPoint.x >= (thumbPos - thumbTouchSize.width) && 
      touchPoint.x <= (thumbPos + thumbTouchSize.width)) 
    } 
} 

現金去這個帖子:http://www.mpatric.com/2009-04-15-more-responsive-sliders-on-the-iphone

+0

'thumbImageForState(UIControlState.Normal)'可以返回零,如果不使用自定義圖像作爲張貼在:http://stackoverflow.com/questions/21313796/how-to-read-size-of-uislider-thumb-image。 Rudolf Adamkovic提出的子類化解決了獲得拇指大小的問題。 – ByteArtisan

+0

謝謝托馬斯!這個答案幫了我很大的時間! – PlateReverb

1

我結束了使用這個,它對我來說工作得很好。

已發佈此答案,以便對某人有所幫助。

import UIKit 

class CustomSlider: UISlider { 

/* 
// Only override draw() if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
override func draw(_ rect: CGRect) { 
    // Drawing code 
} 
*/ 
// Increase slider height 
    override func trackRect(forBounds bounds: CGRect) -> CGRect { 
    let customBounds: CGRect = CGRect(origin: bounds.origin, size: CGSize(width: bounds.size.width, height: 5.0)) 
    return customBounds 
} 



override func thumbRect(forBounds bounds: CGRect, trackRect rect: CGRect, value: Float) -> CGRect { 
    return super.thumbRect(
     forBounds: bounds, trackRect: rect, value: value) 


} 

// Increase Thumb hot spot clickable area 
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool { 
    var bounds: CGRect = self.bounds 
    bounds = bounds.insetBy(dx: -10, dy: -10); 
    return bounds.contains(point); 

} 

override func awakeFromNib() { 
    // Configure Volume slider 
    let thumbImageNormal = UIImage.init(named:"thumb") 
    self.setThumbImage(thumbImageNormal, for: .normal) 

    super.awakeFromNib() 
} 

} 
1

此外,您可以只increese圖像大小,這將導致滑塊拇指大小的增加。

  1. 如果您需要左圖像大小不要太大,但調整滑塊大小隻需在圖像內容周圍添加透明空間。 enter image description here

  2. 如果您在使用的UITableViewCell滑塊的問題還在於,實現代碼如下手柄手勢只是滑塊之前。要改變這種採用如下方案: https://stackoverflow.com/a/37751635/2150954 我想是不是太難了代碼轉換成目標C.

4

斯威夫特3增加觸摸區域

UISlider

子類

class CustomSlider: UISlider { 

    private var thumbTouchSize = CGSize(width: 40, height: 40) 

    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool { 
     let increasedBounds = bounds.insetBy(dx: -thumbTouchSize.width, dy: -thumbTouchSize.height) 
     let containsPoint = increasedBounds.contains(point) 
     return containsPoint 
    } 

    override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool { 
     let percentage = CGFloat((value - minimumValue)/(maximumValue - minimumValue)) 
     let thumbSizeHeight = thumbRect(forBounds: bounds, trackRect:trackRect(forBounds: bounds), value:0).size.height 
     let thumbPosition = thumbSizeHeight + (percentage * (bounds.size.width - (2 * thumbSizeHeight))) 
     let touchLocation = touch.location(in: self) 
     return touchLocation.x <= (thumbPosition + thumbTouchSize.width) && touchLocation.x >= (thumbPosition - thumbTouchSize.width) 
    } 
}