自定义垂直UISlider的问题

时间:2011-08-18 08:48:28

标签: objective-c ios uislider

我正试图在没有拇指的情况下实现垂直滑块,这会对其轨道上的触摸作出反应。我已经将UISlider子类化,并且水平滑块中的一切都很好,但是当我将滑块转换为垂直时,会出现坐标的奇怪东西。可能有其他方法来实现这个?拜托,给我正确的方向,谢谢!

我现在正在使用此代码作为滑块:

@implementation MMTouchSlider

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self];

    self.value = self.minimumValue + (self.maximumValue - self.minimumValue) * (touchLocation.x / self.frame.size.width);

    [super touchesBegan:touches withEvent:event];
}

- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value {

    CGRect thumbRect = [super thumbRectForBounds:bounds trackRect:rect value:value];
    return thumbRect;
}

@end

1 个答案:

答案 0 :(得分:0)

制作垂直滑块很容易 - 只需使用270度旋转!

UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(100, 100, 200, 10)];
float angleInDegrees = 270;
float angleInRadians = angleInDegrees * (M_PI/180);
slider.transform = CGAffineTransformMakeRotation(angleInRadians);
[self.view addSubView:slider];