iOS - 折叠动画UISlider

时间:2013-06-18 16:59:42

标签: ios animation uislider

我想创建一个折叠动画UISlider。基本上,滑块将是拇指图像的大小,直到它被触摸,此时它会在被更改时扩展到完整大小。更改值并松开滑块后,滑块将折叠回原始大小(缩略图的大小)。

我尝试使用touchesBegan和touchesEnd,但这并没有让我走得太远。

到目前为止,我已经将UISlider子类化并覆盖了beginTrackingWithTouch和endTrackingWithTouch。这种代码实现了折叠效果(当然没有动画)但拇指滑块不再改变。关于如何最好地做到这一点的任何想法?

#import "CollapsingUISlider.h"

@implementation CollapsingUISlider


/*-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width       * 4.0f, self.frame.size.height);
[super touchesBegan:touches withEvent:event];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
//  self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width * 0.25f, self.frame.size.height);
[super touchesEnded:touches withEvent:event];
}*/
-(BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
      self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, 400,   self.frame.size.height);
      return YES;
}
-(void)endTrackingWithTouch:(UITouch*)touch withEvent:(UIEvent *)event {
     self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, 20, self.frame.size.height);
}
-(BOOL) continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, 400, self.frame.size.height);
    return self.tracking;
}
@end

1 个答案:

答案 0 :(得分:0)

我最终做了这样的事情:

 UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[platformView addGestureRecognizer:panRecognizer];

-(void)move:(id)sender {
[self.view bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];

if ([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
    firstX = [[sender view] center].x;
    firstY = [[sender view] center].y;
}

translatedPoint = CGPointMake(firstX, firstY+translatedPoint.y);
[[sender view] setCenter:translatedPoint];
if (platformView.center.y < platformCenter.y - offset) {
    platformView.center = CGPointMake(platformCenter.x,platformCenter.y-offset);
    ((UIPanGestureRecognizer*)sender).enabled = NO;
} else if (platformView.center.y > platformCenter.y) {
    platformView.center = CGPointMake(platformCenter.x,platformCenter.y);
    ((UIPanGestureRecognizer*)sender).enabled = NO;
}
}