0
我想讓UISlider響應輕擊minimumValueImage
和/或maximumValueImage
,將該值設置爲最小值或最大值。我似乎無法找到這種情況下的「正常」方法,所以我想出了這個解決方案。我正在對UISlider進行分類,並保存用戶開始觸摸的位置。通過比較位置,我可以看出它是否在其中一幅圖像上。工作好,但有沒有一種不太自定義的方式來實現相同的目標?使用minimumImageValue/maximumImageValue更改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
有沒有得到一個答案呢? – Gujamin
可悲的是,沒有。但是我的實施工作,所以我已經解決了。 – fguchelaar
我已經把解決方案放在github上了,對於誰有興趣:https://github.com/fguchelaar/FGSlider – fguchelaar