iOS UIButton缩放动画

时间:2013-08-12 09:46:08

标签: ios uibutton transformation uianimation

我正在研究UIButton动画,它沿着x轴移动,同时从初始大小缩放到原始大小。当我尝试下面的代码时,它不会移动到它应该去的地方,并且不会扩展到它的原始大小。

这是我初始化按钮的代码:

 _testBtn1.layer.anchorPoint = CGPointMake(0.5f, 0.5f);
scaleBtn = CGAffineTransformMakeScale(0.2, 0.2);
[_testBtn1 setTransform: scaleBtn];

这是我的移动/翻译和缩放代码:

CGAffineTransform translate = CGAffineTransformMakeTranslation(50.0f, 0.0f);
CGAffineTransform animate = CGAffineTransformConcat(scaleBtn, translate);
[_testBtn1 setTransform:animate];

任何帮助,建议,建议将不胜感激。我是iOS的新手。谢谢!

4 个答案:

答案 0 :(得分:1)

您可以创建自定义类型UIButton(通过将类型更改为自定义,或使用

编程)来创建自定义类型UIButton
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];//set the button's image with 
[aButton setImage:[UIImage imageNamed:@"abc" forState:UIControlStateNormal];

要移动它,只需正常设置其位置......

[UIView animateWithDuration:0.5 animations:^{aButton.center = newCenter;}];

或者

CGRect originalFrame = aButton.frame;
aButton.frame = CGRectMake(originalFrame.origin.x, originalFrame.origin.y, originalFrame.size.width, 0);
[UIView animateWithDuration:0.5 animations:^{aButton.frame = originalFrame;}];

或提供此链接 http://objectiveapple.blogspot.in/2011/10/23-quizapp-16-animate-scale-uibutton.html

答案 1 :(得分:0)

这应该可以通过Core Animation轻松完成,看看最基本的CABasicAnimation

答案 2 :(得分:0)

一个简单的缩放(弹性)动画,在swift中实现完成处理程序。

希望它能以某种方式帮助你或其他任何人。

viewToanimate.transform = CGAffineTransform(scaleX: 0.1, y: 0.1) UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 6.0, animations: { _ in viewToAnimate.transform = .identity }, completion: { _ in //Implement your awesome logic here. })

答案 3 :(得分:0)

我制作了这个动画:

enter image description here

使用此可重用的类:

class AnimatedButton: UIButton {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addTargets()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        addTargets()
    }
    
    func addTargets() {
        self.addTarget(self, action: #selector(scaleDownAnimated), for: .touchDown)
        self.addTarget(self, action: #selector(scaleBackToNormalAnimated), for: [.touchUpOutside, .touchCancel, .touchUpInside])
    }
    
    @objc func scaleDownAnimated() {
        UIView.animate(withDuration: 0.25, delay: 0, options: .allowUserInteraction) {
            self.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)
        } completion: { _ in }
    }
    
    @objc func scaleBackToNormalAnimated() {
        UIView.animate(withDuration: 0.2, delay: 0, options: .allowUserInteraction) {
            self.transform = CGAffineTransform(scaleX: 1, y: 1)
        } completion: { _ in }
    }
}

然后只需按如下所示设置按钮AnimatedButton的类(如果您使用的是Storyboard),然后正常使用该按钮:

enter image description here