悬停动画上的 UIButton 比例 - iOS Objective-C

UIButton scale on hover animation - iOS Objective-C

所以我有一个 UIButton,我希望它在用户触摸内部时放大,但如果用户使用 UIView 动画将手指拖走则缩小。

提前致谢!

在 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];
-(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;
        }];
    }];
}