【问题标题】:Animating the heigh of a UIButton with UIGestureRecognizer使用 UIGestureRecognizer 为 UIButton 的高度设置动画
【发布时间】:2015-02-13 15:51:38
【问题描述】:

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

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

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

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

我正在使用情节提要和自动布局。 最好的方法是什么?

谢谢!

【问题讨论】:

  • 你在使用自动布局吗?
  • @YuviGr 是的,我正在使用自动布局
  • @YuviGr 所需行为的一个很好的例子是 iOS 操作中心

标签: ios objective-c animation uiview uigesturerecognizer


【解决方案1】:

您需要使用按钮的高度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;
  }
}

编辑

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

但是你真的需要阅读 UIPanGestureRecognizerUIGestureRecognizer 的文档才能真正理解并更好地在 iOS 中编码

EDIT2

弹跳效果

看看这个answer

【讨论】:

  • 我收到错误:使用未声明的标识符“开始” - 我需要导入某些内容还是符合某些内容?
  • 我应该使用 UIGestureRecognizer 的什么属性来更改约束的常量属性?
  • 检查我的编辑,就像我说的检查我给你的链接中的文档。这将帮助你
  • 我已经阅读了它,但我没有看到完整的图片,我不知道如何去创建反弹效果
猜你喜欢
  • 1970-01-01
  • 2015-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-27
  • 1970-01-01
相关资源
最近更新 更多