Saturday, 8 September 2018

Animation

Animation interaction is best way to deal with different type of interact with application functionality. Now a days Animation is big part of Application for success.
In this article you can create easy way to animated object in Swift Xcode.
If you want to object zoomin, zoomout, move, roted. bounce etc.,

Example of code :

import UIKit

enum Axis {
    case x
    case y
    case both
}
extension UIView{
    func zoomAnimation(withDuration duration: Double, withZoomLevel zoomLevel: CGFloat, complition : ((Bool) -> Swift.Void)?)  {
        UIView.animate(withDuration: duration, animations: {
            self.transform = CGAffineTransform(scaleX: zoomLevel, y: zoomLevel)
        }) { _ in
            UIView.animate(withDuration: duration, animations: {
                self.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
            },completion : {_ in
                complition?(true)
            })
        }
    }
    
    func rotedAnimation(withDuration duration: Double,withAngle angle : CGFloat,withRepetation isRepeat:Bool , complition : ((Bool) -> Swift.Void)?) {
        if isRepeat
        {
            UIView.animate(withDuration: duration, delay: 0.0, options: .repeat, animations: {
                self.transform = self.transform.rotated(by: angle)
            }) { _ in
                complition?(true)
            }
        }
        else
        {
            UIView.animate(withDuration: duration, animations: {
                self.transform = self.transform.rotated(by: angle)
            }, completion: {_ in
                complition?(true)
            })
        }
    }
    
    func moveAnimation(withDuration duration : Double,withAxis axis:Axis,withAnimStepCount step:CGFloat,complition : ((Bool) -> Swift.Void)?) {
        let oldFrame = self.frame
        UIView.animate(withDuration: duration, animations: {
            if axis == .both
            {
                self.frame.origin.x += step
                self.frame.origin.y += step
            }
            else if axis == .y
            {
                self.frame.origin.y += step
            }
            else
            {
                self.frame.origin.x += step
            }
        }) { _ in
            UIView.animate(withDuration: duration, animations: {
                self.frame = oldFrame
            }, completion: { _ in
                complition?(true)
            })
        }
    }
    
    func bounceAnimation(withDuration duration : Double,withAxis axis:Axis,withBounceStepCount step:Int,withAxisStepCount bounceStep : CGFloat ,complition : ((Bool) -> Swift.Void)?) {
        let oldFrame = self.frame
        UIView.animate(withDuration: duration/Double(step), animations: {
            if axis == .x
            {
                self.frame.origin.x  += bounceStep
            }
            else
            {
                self.frame.origin.y += bounceStep
            }
        }, completion: { _ in
            UIView.animate(withDuration: duration/Double(step), animations: {
                if axis == .x
                {
                    self.frame.origin.x -= (bounceStep * 2)
                }
                else
                {
                    self.frame.origin.y -= (bounceStep * 2)
                }
            },completion:{_ in
                complition?(true)
                UIView.animate(withDuration: duration/Double(step), animations: {
                    self.frame.origin.x = oldFrame.origin.x
                    self.frame.origin.y = oldFrame.origin.y
                    
                },completion:{_ in
                    complition?(true)})
            })
        })
    }
}


You can put above code in file and easy to use
-> for example zoom-in object then

obj.zoomAnimation(withDuration: 1.0, withZoomLevel: 0.5, complition: nil)

-> zoom-out

obj.zoomAnimation(withDuration: 1.0, withZoomLevel: 1.5 ,complition: {_ in
            print("zoom complition")
        })

-> Bounce 

        sender.bounceAnimation(withDuration: 1.0, withAxis: .x, withBounceStepCount: 10, withAxisStepCount: 20, complition: nil)


For more info watch this video for different animation 
Github project link is available in video description 




Another example of Add to cart animation 



If you like my video then please like and subscribe and also share with iOS community.

Thank you.





No comments:

Post a Comment