【发布时间】:2013-06-18 16:59:42
【问题描述】:
我想创建一个折叠动画 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
【问题讨论】:
-
问题是当您开始触摸时,您已经开始与控件交互,这意味着它可能正在跟踪值的变化。如果您同时调整控件的大小,您会发现这不是一个好主意。也可能以奇怪的方式行事。
-
返回值(特别是
self.tracking)可能有问题。我认为您可以通过更改子视图框架来更轻松、更安全地进行操作,否则布局将被破坏。