Animating only the image in UIBarButtonItem

后端 未结 4 664
暖寄归人
暖寄归人 2021-01-03 07:11

Ive seen this effect in 2 apps and I REALLY want to find how to do it.

The animation is in a UIBarButtonItem, and is only to the image. The image is a + symbol, and

4条回答
  •  悲&欢浪女
    2021-01-03 07:29

    Recently had to do the same thing in Swift. I created a tutorial that includes starter and final projects, and goes step-by-step with some tips sprinkled in. The code looks like this:

    @IBOutlet weak var rightBarButton: UIBarButtonItem! {
        didSet {
            let icon = UIImage(named: "star")
            let iconSize = CGRect(origin: CGPointZero, size: icon!.size)
            let iconButton = UIButton(frame: iconSize)
            iconButton.setBackgroundImage(icon, forState: .Normal)
            rightBarButton.customView = iconButton
            rightBarButton.customView!.transform = CGAffineTransformMakeScale(0, 0)
    
            UIView.animateWithDuration(1.0,
                delay: 0.5,
                usingSpringWithDamping: 0.5,
                initialSpringVelocity: 10,
                options: .CurveLinear,
                animations: {
                    self.rightBarButton.customView!.transform = CGAffineTransformIdentity
                },
                completion: nil
            )    
    
            iconButton.addTarget(self, action: "tappedRightButton", forControlEvents: .TouchUpInside)        
        }
    }
    
    func tappedRightButton(){
        rightBarButton.customView!.transform = CGAffineTransformMakeRotation(CGFloat(M_PI * 6/5))
        UIView.animateWithDuration(1.0) {
            self.rightBarButton.customView!.transform = CGAffineTransformIdentity
        }
    }
    

提交回复
热议问题