使用 minimumImageValue / maximumImageValue 更改 UISlider 值
Posted
技术标签:
【中文标题】使用 minimumImageValue / maximumImageValue 更改 UISlider 值【英文标题】:Change UISlider value using minimumImageValue / maximumImageValue 【发布时间】:2012-10-31 13:12:16 【问题描述】:我想让 UISlider 响应点击minimumValueImage
和/或maximumValueImage
,将值设置为最小值或最大值。对于这种情况,我似乎找不到“正常”的方法,所以我想出了这个解决方案。我正在继承 UISlider 并记录用户开始触摸的位置。通过比较位置,我可以确定它是否在其中一张图像上。工作正常,但有没有更不习惯的方法来实现相同的目标?
@interface FGSlider ()
@property (nonatomic) CGRect minimumValueImageRect;
@property (nonatomic) CGRect maximumValueImageRect;
@property (nonatomic) BOOL touchesBeganInMinimumValueImageRect;
@property (nonatomic) BOOL touchesBeganInMaximumValueImageRect;
@end
@implementation FGSlider
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
[super touchesBegan:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
if(CGRectContainsPoint(self.minimumValueImageRect, location))
self.touchesBeganInMinimumValueImageRect = YES;
else if(CGRectContainsPoint(self.maximumValueImageRect, location))
self.touchesBeganInMaximumValueImageRect = YES;
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
[super touchesEnded:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
if(self.touchesBeganInMinimumValueImageRect && CGRectContainsPoint(self.minimumValueImageRect, location))
[self setValue:self.minimumValue animated:YES];
[self sendActionsForControlEvents:UIControlEventValueChanged];
else if(self.touchesBeganInMaximumValueImageRect && CGRectContainsPoint(self.maximumValueImageRect, location))
[self setValue:self.maximumValue animated:YES];
[self sendActionsForControlEvents:UIControlEventValueChanged];
// reset state
self.touchesBeganInMinimumValueImageRect = NO;
self.touchesBeganInMinimumValueImageRect = NO;
-(CGRect)minimumValueImageRectForBounds:(CGRect)bounds
self.minimumValueImageRect = [super minimumValueImageRectForBounds:bounds];
return self.minimumValueImageRect;
-(CGRect)maximumValueImageRectForBounds:(CGRect)bounds
self.maximumValueImageRect = [super maximumValueImageRectForBounds:bounds];
return self.maximumValueImageRect;
@end
【问题讨论】:
曾经得到这个答案吗? 很遗憾,没有。但是我的实现是有效的,所以我已经解决了。 我已经把解决方案放到了github上,有兴趣的朋友:github.com/fguchelaar/FGSlider 【参考方案1】:我创建了一个简单的类别,它似乎可以工作并且不依赖于计算界限,并且比您的实现简单得多。试试看,让我知道你的想法。
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
UITouch *touch = [touches anyObject];
for (UIView *view in self.subviews)
CGPoint locationPoint = [touch locationInView:view];
if ([view pointInside:locationPoint withEvent:event])
UIImageView *imageView = (UIImageView*)view;
if (imageView.image == self.maximumValueImage)
[self setValue:self.maximumValue animated:YES];
[self sendActionsForControlEvents:UIControlEventValueChanged];
else if (imageView.image == self.minimumValueImage)
[self setValue:self.minimumValue animated:YES];
[self sendActionsForControlEvents:UIControlEventValueChanged];
[super touchesBegan:touches withEvent:event];
【讨论】:
以上是关于使用 minimumImageValue / maximumImageValue 更改 UISlider 值的主要内容,如果未能解决你的问题,请参考以下文章