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