悬停动画上的UIButton缩放 - iOS Objective-C

时间:2015-07-17 00:25:28

标签: ios uibutton uiviewanimation

所以我有一个UIButton,我想让它在用户触摸时扩大,但如果用户使用UIView动画将手指拉开,则缩小比例。

提前致谢!

2 个答案:

答案 0 :(得分:1)

在UIControlEventTouchDown上,您想要增加按钮的大小,然后在UIControlEventTouchDragExit上,您想要再次减小它。你可以这样做:

-(IBAction)touchDown:(UIButton*)sender{
    [UIView animateWithDuration:0.1f animations:^{
        sender.transform = CGAffineTransformMakeScale(1.2f, 1.2f);
    }];
}

-(IBAction)touchDragExit:(UIButton*)sender{
    [UIView animateWithDuration:0.1f animations:^{
        sender.transform = CGAffineTransformIdentity;
    }];
}

当然,您可能还希望在触摸,触摸取消等时减小按钮的大小。

确保将按钮连接到这些方法,或者在Interface Builder中(正如我在我的示例中假设的那样,因此是IBActions),或者在创建按钮时在代码中连接:

[button addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:@selector(touchDragExit:) forControlEvents:UIControlEventTouchDragExit];

答案 1 :(得分:0)

-(void)buttonScaleAnimation:(UIButton *)sender {
    float animationDuration = 0.25;
    // Increase scale to 1.25 with animation
    [UIView animateWithDuration:animationDuration animations:^{
        sender.transform = CGAffineTransformMakeScale(1.25, 1.25);
    } completion:^(BOOL finished) {
        // Return to original scale with animation
        [UIView animateWithDuration:animationDuration animations:^{
            sender.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            sender.transform = CGAffineTransformIdentity;
        }];
    }];
}