2012-10-31 45 views
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 
+0

有沒有得到一個答案呢? – Gujamin

+0

可悲的是,沒有。但是我的實施工作,所以我已經解決了。 – fguchelaar

+0

我已經把解決方案放在github上了,對於誰有興趣:https://github.com/fguchelaar/FGSlider – fguchelaar

回答

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]; 
}