简体   繁体   English

iOS UIButton 缩放动画

[英]iOS UIButton scale animation

i'm working on a UIButton animation where it moves along x-axis while it scales to its original size from the initial size.我正在处理一个 UIButton 动画,它沿着 x 轴移动,同时从初始大小缩放到其原始大小。 when i tried the code below it doesnt move to the point where it should go and doesn't scale up to its original size.当我尝试下面的代码时,它不会移动到它应该去的地方,也不会放大到其原始大小。

this is my code for initializing the button:这是我初始化按钮的代码:

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

and this is my code for the moving/translation and scaling:这是我的移动/平移和缩放代码:

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

any help, suggestion, advice will be appreciated.任何帮助,建议,意见将不胜感激。 i'm new to iOS..thanks!我是 iOS 新手……谢谢!

You can just create a custom type UIButton (either with IB by changing the type to Custom, or programmatically with您可以创建一个自定义类型的 UIButton(使用 IB 通过将类型更改为自定义,或以编程方式使用

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];//set the button's image with 
[aButton setImage:[UIImage imageNamed:@"abc" forState:UIControlStateNormal];

To move it, just animate its position normally...要移动它,只需正常设置其位置的动画...

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

Or要么

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

OR Reffer this link http://objectiveapple.blogspot.in/2011/10/23-quizapp-16-animate-scale-uibutton.html或参考此链接http://objectiveapple.blogspot.in/2011/10/23-quizapp-16-animate-scale-uibutton.html

这应该很容易用 Core Animation 完成,看看最基础的一个CABasicAnimation

A simple scale(bouncy) animation with completion handler implementation in swift.一个简单的缩放(有弹性)动画,在 swift 中完成处理程序实现。

Hope it'll help you or anyone else in some way.希望它会以某种方式帮助您或其他任何人。

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. })

I made this animation:我做了这个动画:

在此处输入图片说明

With this reusable class:有了这个可重用的类:

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 }
    }
}

Then just make the class of your button AnimatedButton like below (if you are using Storyboards) and use the button normally:然后只需将按钮的类AnimatedButton如下所示(如果您使用的是 Storyboards)并正常使用按钮:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM