使用UIGestureRecognizer动画UIButton的高度

时间:2015-02-13 15:51:39

标签: ios objective-c animation uiview uigesturerecognizer

我正在构建一个应用程序,我想要一种反向拉动刷新。没有涉及tableView,但我想拉一个UIButton来增加它的高度并同时为标签和其他属性设置动画,例如backgroundColor - 实现与pull-to-refresh相同的效果。我还需要反弹/反弹效果来模拟重力。

所需行为的一个很好的例子是iOS行动中心

一个想法是将UIView的animateWithDuration链接到UIGestureRecognizer:

[UIView animateWithDuration: delay: usingSpringWithDamping: initialSpringVelocity: options: animations: completion:nil]

我正在使用故事板和自动布局。 最好的办法是什么?

谢谢!

1 个答案:

答案 0 :(得分:0)

您需要使用按钮的高度constraint

如果你没有任何参考,你可以控制 - 拖动它到ViewController,像这样

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *buttonHeight;

完成此操作后,您可以根据用户平移手势更改按钮高度。只需将gestureRecogniser添加到视图

即可
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(userPulledDown:)];
[view addGestureRecognizer:pan];
手势方法中的

- (void)userPulledDown:(UIGestureRecognizer *)gesture
{
  switch (gesture.state){
    case UIGestureRecognizerStateBegan:
       // the user has started to pull the view
       break;
    case UIGestureRecognizerStateChanged:
       // create the animation of the button (using the constraint) & other views
       break;
    case UIGestureRecognizerStateEnded:
       // clean-up after the user released the gesture
       break;
  }
}

修改

我建议您在translationInView:中调用方法UIPanGestureRecognizer此方法会在您给出的视图中返回panGesture的CGPoint(x,y)。

但你真的需要阅读UIPanGestureRecognizer&的文档。 UIGestureRecognizer要真正理解并在iOS中进行更好的编码

<强> EDIT2

弹跳效果

查看此answer