iOS - 折叠动画 UISlider

Posted

技术标签:

【中文标题】iOS - 折叠动画 UISlider【英文标题】:iOS - Collapsing Animated UISlider 【发布时间】: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)可能有问题。我认为您可以通过更改子视图框架来更轻松、更安全地进行操作,否则布局将被破坏。 【参考方案1】:

我最终做了这样的事情:

 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;


【讨论】:

以上是关于iOS - 折叠动画 UISlider的主要内容,如果未能解决你的问题,请参考以下文章

展开和折叠动画在 ios6 中不起作用

ios 11 UITableViewHeaderFooterView 无法正确滚动动画

iOS 动画视图,包括子视图

UITableViewCell 折叠时展开和折叠动画不起作用

UItableViewCell 展开/折叠动画(展开时有效,但不折叠)

vue实现collapse折叠板动画,可设置动画时间,动画延迟等