5

In swift, I am transitioning an object in to the view, and I need it to slide in, or fade in. How can I obtain this type of animation in my program?

I have all my view elements made programmatically without IB (Interface Builder)

Is there documentation I can look at for reference? I could not find any.

4
  • 1
    Same way as in Objective C. Commented Jun 8, 2014 at 20:54
  • I can't find anything with the type of animation that I want. I just want maybe a simple slide in or fade in. Not an OpenGL game. Commented Jun 8, 2014 at 20:55
  • Look up UIView's animate methods. They work the same way in Swift as they did in Obj-C. Commented Jun 8, 2014 at 21:17
  • 1
    For slide or fade animations you want a CABasicAnimation or CAKeyframeAnimation, where you set the x/y position or the opacity via the animation. See documentation or other questions showing how to do it in Objective-C, there is plenty of it. If you get stuck, update this question with what you tried to do and a description of what isn't working. Commented Jun 8, 2014 at 21:55

4 Answers 4

15

I'm used to using [UIView animateWithDuration ...], but I found it a bit tricky switching the block syntax over to Swift at first. Here's a quick lazy code:

view.alpha = 0.0 UIView.animateWithDuration(0.25, animations: { view.alpha = 1.0 }, completion: { (value: Bool) in println(">>> Animation done.") }) 
Sign up to request clarification or add additional context in comments.

2 Comments

can you pls tell me how to animate continously..??
Perhaps try something like stackoverflow.com/questions/28644335/…
4

Or to animate the position appearing from the left of the screen:

// some variables for the UIView let width = 200 let height = 100 let yPosition = 10 // create view and position it offscreen to the left let view = UIView() // you could have also used the new more verbose Swift way of making a CGRect: // CGRect(x: xValue, y: yValue, width: widthValue, height: heightValue) view.frame = CGRectMake(-width, yPosition, width, height) view.backgroundColor = UIColor.blueColor() // etc... // animation position over 1.0 second duration UIView.animateWithDuration(1.0, animations: { view.frame = CGRectMake(0, yPosition, width, height) }) 

If you're completely new to the UIView animation APIs I wrote post that covers a bunch of animation options in swift here: http://mathewsanders.com/prototyping-iOS-iPhone-iPad-animations-in-swift/

Comments

0

Swift 3 & 4 Please Add necessary objects like tableView

import UIKit class SecondViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { @IBOutlet var tabl: UITableView! var name = ["tony","abu"] override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return name.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell() cell.textLabel?.text = name[indexPath.row] return cell } override func viewWillAppear(_ animated: Bool) { tabl.center.x = self.view.frame.width/5 UIView.animate(withDuration: 1.0, delay: 0, usingSpringWithDamping: 1.0, initialSpringVelocity:0.45, options: [], animations: ({ self.tabl.center.x = self.view.frame.width/3 //self.buttonAction.center.x = self.view.frame.width/2 }), completion: nil) } } 

Comments

-1

Apples Core animation seen here https://developer.apple.com/library/Mac/DOCUMENTATION/Cocoa/Conceptual/CoreAnimation_guide/Introduction/Introduction.html its very fun. youtube it

1 Comment

Link is broken hence -1

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.