Transform animation initial solution in Swift

Source: Internet
Author: User



Create an interface, just like this:






The top is a Uisegmentcontrolthat is used to make the type of transform. respectively:cgaffinetransformmaketranslation,cgaffinetransformtranslate, Cgaffinetransformidentity.



Then there is a UILabel, this label displays in real time what the current animation type is.



Orange is the view of the animation.



The bottom is a button that presses this button orange view to start the animation.



The other, blue lines are the constraints of these few view. Specify how these view positions are positioned, for example, relative to the top, distance from the left, distance to the right, and so on.



After you have configured the view on the page, specify the corresponding object in the controller for those view. and specify the individual view events in the nib file.



The object that the view corresponds to in the controller:


@IBOutlet weak var animationView: UIView!
    @IBOutlet weak var animationLabel: UILabel!


Uisegmentcontrol Events:


 
 @IBAction func segmentAction(sender: AnyObject) {
        var segmentControl = sender as UISegmentedControl
        animationType = segmentControl.selectedSegmentIndex
    }


To run an animated button event:


 
 
@IBAction func runAction(sender: AnyObject) {
        var distance: CGFloat = 30
        switch animationType {
        case 0:
            self.animationLabel.text = "CGAffineTransformMakeTranslation"
            UIView.animateWithDuration(1.0, animations: {
                self.animationView.transform = CGAffineTransformMakeTranslation(distance, 0)
            })
        case 1:
            self.animationLabel.text = "CGAffineTransformTranslate"
            UIView.animateWithDuration(1.0, animations: {
                self.animationView.transform = CGAffineTransformTranslate(self.animationView.transform, distance, 0)
            })
        case 2:
            self.animationLabel.text = "CGAffineTransformIdentity"
            UIView.animateWithDuration(1.0, animations: {
                self.animationView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, distance, 0)
            })
        default:
            println("")
        }
    }





In viewdidload setting the type of animation selected, here we do not directly take the value of Uisegmentcontrol SelectedIndex. Therefore, setting the default value within the viewdidload method is 1 (that is, selected to be the first).



All code:


 
//
// ViewController.swift
// TransformDemo
//
// Created by Bruce Lee on 30/11/14.
// Copyright (c) 2014 Dynamic Cell. All rights reserved.
//
// QQ: 1828099940, Group: 58099570 Welcome to join the discussion
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var animationView: UIView!
    @IBOutlet weak var animationLabel: UILabel!
    
    var animationType: Int!
    
    override func viewDidLoad () {
        super.viewDidLoad ()
        
        animationType = 0
    }

    override func didReceiveMemoryWarning () {
        super.didReceiveMemoryWarning ()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func segmentAction (sender: AnyObject) {
        var segmentControl = sender as UISegmentedControl
        animationType = segmentControl.selectedSegmentIndex
    }

    @IBAction func runAction (sender: AnyObject) {
        var distance: CGFloat = 30
        switch animationType {
        case 0:
            self.animationLabel.text = "CGAffineTransformMakeTranslation"
            UIView.animateWithDuration (1.0, animations: {
                self.animationView.transform = CGAffineTransformMakeTranslation (distance, 0)
            })
        case 1:
            self.animationLabel.text = "CGAffineTransformTranslate"
            UIView.animateWithDuration (1.0, animations: {
                self.animationView.transform = CGAffineTransformTranslate (self.animationView.transform, distance, 0)
            })
        case 2:
            self.animationLabel.text = "CGAffineTransformIdentity"
            UIView.animateWithDuration (1.0, animations: {
                self.animationView.transform = CGAffineTransformTranslate (CGAffineTransformIdentity, distance, 0)
            })
        default:
            println ("")
        }
    }
}





Transform animation initial solution in Swift


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.