0

minimumValueImageUISlider を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
4

1 に答える 1

0

機能しているように見え、境界の計算に依存せず、実装よりもはるかに単純な単純なカテゴリを作成しました。試してみて、あなたの考えを教えてください。

- (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];
}
于 2013-07-23T21:21:14.590 に答える