minimumValueImage
UISlider をand/orのタップに応答させmaximumValueImage
、値を最小または最大に設定したいと思います。このシナリオの「通常の」アプローチが見つからないように見えるため、このソリューションを思いつきました。UISlider をサブクラス化し、ユーザーがタッチを開始した場所を登録します。場所を比較することで、それが画像の 1 つにあったかどうかを判断できます。問題なく動作しますが、同じ目標を達成するためのよりカスタムな方法はありますか?
@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