UIAnimate只在按钮上发生一次 [英] UIAnimate only happens once on button

查看:88
本文介绍了UIAnimate只在按钮上发生一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用中有一个带有背景图片的按钮。按下按钮时,我希望它旋转90度。下面的代码是我的IBAction。它第一次完美地工作,但每次按下按钮它都不会旋转。

I have a button in my app that has a background image. When the button is pressed I want it to rotate 90 degrees. The code below is my IBAction. It works perfectly the first time, but every other time I press the button it doesn't rotate.

@IBAction func addButton(_ sender: UIButton) {
    UIView.animate(withDuration: 0.05, animations: ({
        sender.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi / 2))
    }))
}


推荐答案

您始终将变换设置为90度。如果您想在每次点击按钮时再旋转90度,则需要更新现有变换。

You always set the transform to 90 degrees. If you want to rotate an additional 90 degrees each time you tap the button you need to update the existing transform.

sender.transform = sender.transform.rotated(by: CGFloat(Double.pi / 2))

这篇关于UIAnimate只在按钮上发生一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆