I have created this animation for the first screen of the app(I'm not using Launchscreen) but when the animation ends I don't know how to pass to the next view controller. I've tired by segue but nothing :/
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var logoLSMini: UIImageView!
#IBOutlet weak var logoLSMini2: UIImageView!
#IBOutlet weak var logoLS: UIImageView!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.logoLSMini.alpha = 0.0
self.logoLSMini2.alpha = 0.0
self.logoLS.alpha = 0.0
self.logoLS.frame.origin.y = +100
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIView.animate(withDuration: 5, delay: 0.0, options: .curveEaseOut , animations: {
//FIRST EFFECT
self.logoLSMini.alpha = 1.0
self.logoLSMini.transform = CGAffineTransform(rotationAngle: 360)
self.logoLSMini.transform = CGAffineTransform(scaleX: 100, y: 100)
self.logoLS.alpha = 1.0
}, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
You simply need to perform the segue in the completion of UIView.animate. Just make sure you substitute whatever identifier you added to your segue in Storyboard for yourSegue.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIView.animate(withDuration: 5, delay: 0.0, options: .curveEaseOut , animations: {
//FIRST EFFECT
self.logoLSMini.alpha = 1.0
self.logoLSMini.transform = CGAffineTransform(rotationAngle: 360)
self.logoLSMini.transform = CGAffineTransform(scaleX: 100, y: 100)
self.logoLS.alpha = 1.0
}, completion: { _ in
self.performSegue(withIdentifier: "yourSegue", sender: nil)
})
}
Try to put the code of moving viewController in the completionHandler
UIView.animate(withDuration: 5, delay: 0.0, options: .curveEaseOut, animations: {
.....
}, completion: { (finished) in
if finished {
//Move to your next controller
DispatchQueue.main.async {
//Your code
}
}
})
Related
I have created simple animation show a coin pulsating and it worked pretty fine, until I added viewcontroller before the one with animation on storyboard.
I wanted to have menu screen in my game before showing the screen with this animation.
This is the code of the animation:
let imageView: UIImageView = coinImageView[0]
UIView.animate(withDuration: 0.5,
delay: 0.1,
options: [
.repeat,
.autoreverse,
.curveEaseIn,
.allowUserInteraction
],
animations: {
imageView.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
}
)
The first view (with menu) looks like this:
class MenuViewController: UIViewController {
#IBAction func newGameButton(_ sender: Any) {
let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController") as! ViewController
self.present(viewController, animated: true, completion: nil)
viewController.newGame()
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
and the second viewcontroller (the one with animation) looks like this:
class ViewController: UIViewController, SavingViewControllerDelegate, UICollectionViewDelegate, UICollectionViewDataSource {
var coinImageView: [UIImageView] = []
func grantCoins(n: Int) {
let coinCount = coin.count
for i in coinCount...(coinCount+n) {
coin.append(UIImage(named: "coin.png")!)
coinImageView.append(UIImageView(image: coin[i]))
coinImageView[i].isUserInteractionEnabled = true
coinImageView[i].addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.handlePan(_:))))
coinImageView[i].frame = CGRect(x: 147, y: -100, width: 60, height: 60)
view.addSubview(coinImageView[i])
let finalPoint = CGPoint(
x: 177,
y: 278
)
generateNewCoin(gestureView: coinImageView[i])
}
view.bringSubviewToFront(coinImageView[0])
}
func newGame() {
animateFirstCoin()
}
func animateFirstCoin() {
let imageView: UIImageView = coinImageView[0]
UIView.animate(
withDuration: 0.5,
delay: 0.1,
options: [.repeat, .autoreverse, .curveEaseIn, .allowUserInteraction],
animations: {
imageView.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
})
}
}
Is there any obvious explanation for the problem?
After a day of fruitless research I have found it finally.
When first coins were generated the view was not visible yet, to the animation didn't work. Another batch of coins was animated fine.
I just put this code in my function to check this out:
if (view.window == nil) {
print("view is not visible")
} else {
print("view is visible")
}
Thanks
I am using this library called SwiftyCam As it is mentioned in its page, this part is a bit confusing:
SwiftyCam is a drop-in convenience framework. To create a Camera instance, create a new UIViewController subclass. Replace the UIViewController subclass declaration with SwiftyCamViewController.
So i created a new view controller extends class SwiftyCameraViewController: SwiftyCamViewController, SwiftyCamViewControllerDelegate as in sample project. And i called this class from my main UIViewcontroller(MainViewController.swift) as:
let swiftyCamera = SwiftyCameraViewController()
self.present(swiftyCamera, animated: true, completion: nil)
So, this is SwiftyCameraViewController:
import UIKit
class SwiftyCameraViewController: SwiftyCamViewController, SwiftyCamViewControllerDelegate {
#IBOutlet weak var captureButton: SwiftyRecordButton!
#IBOutlet weak var flipCameraButton: UIButton!
#IBOutlet weak var flashButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
cameraDelegate = self
maximumVideoDuration = 10.0
shouldUseDeviceOrientation = true
allowAutoRotate = true
audioEnabled = true
}
override var prefersStatusBarHidden: Bool {
return true
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
captureButton.delegate = self
}
func swiftyCam(_ swiftyCam: SwiftyCamViewController, didTake photo: UIImage) {
let newVC = PhotoViewController(image: photo)
self.present(newVC, animated: true, completion: nil)
}
func swiftyCam(_ swiftyCam: SwiftyCamViewController, didBeginRecordingVideo camera: SwiftyCamViewController.CameraSelection) {
print("Did Begin Recording")
captureButton.growButton()
UIView.animate(withDuration: 0.25, animations: {
self.flashButton.alpha = 0.0
self.flipCameraButton.alpha = 0.0
})
}
func swiftyCam(_ swiftyCam: SwiftyCamViewController, didFinishRecordingVideo camera: SwiftyCamViewController.CameraSelection) {
print("Did finish Recording")
captureButton.shrinkButton()
UIView.animate(withDuration: 0.25, animations: {
self.flashButton.alpha = 1.0
self.flipCameraButton.alpha = 1.0
})
}
func swiftyCam(_ swiftyCam: SwiftyCamViewController, didFinishProcessVideoAt url: URL) {
let newVC = VideoViewController(videoURL: url)
self.present(newVC, animated: true, completion: nil)
}
func swiftyCam(_ swiftyCam: SwiftyCamViewController, didFocusAtPoint point: CGPoint) {
let focusView = UIImageView(image: #imageLiteral(resourceName: "focus"))
focusView.center = point
focusView.alpha = 0.0
view.addSubview(focusView)
UIView.animate(withDuration: 0.25, delay: 0.0, options: .curveEaseInOut, animations: {
focusView.alpha = 1.0
focusView.transform = CGAffineTransform(scaleX: 1.25, y: 1.25)
}, completion: { (success) in
UIView.animate(withDuration: 0.15, delay: 0.5, options: .curveEaseInOut, animations: {
focusView.alpha = 0.0
focusView.transform = CGAffineTransform(translationX: 0.6, y: 0.6)
}, completion: { (success) in
focusView.removeFromSuperview()
})
})
}
func swiftyCam(_ swiftyCam: SwiftyCamViewController, didChangeZoomLevel zoom: CGFloat) {
print(zoom)
}
func swiftyCam(_ swiftyCam: SwiftyCamViewController, didSwitchCameras camera: SwiftyCamViewController.CameraSelection) {
print(camera)
}
func swiftyCam(_ swiftyCam: SwiftyCamViewController, didFailToRecordVideo error: Error) {
print(error)
}
#IBAction func cameraSwitchTapped(_ sender: Any) {
switchCamera()
}
#IBAction func toggleFlashTapped(_ sender: Any) {
flashEnabled = !flashEnabled
if flashEnabled == true {
flashButton.setImage(#imageLiteral(resourceName: "flash"), for: UIControlState())
} else {
flashButton.setImage(#imageLiteral(resourceName: "flashOutline"), for: UIControlState())
}
}
}
It crashed at line 33: captureButton.delegate = self, because captureButton is nil
I appreciate any kind of help. Thank you.
You must load the viewController with identifier
let swiftyCamera = self.storyboard?.instantiateViewController(withIdentifier:"swiftyCameraID")
self.present(swiftyCamera, animated: true, completion: nil)
I write marquee text for first view controller but change the controller and back the first view controller marquee text is not working.
First Controller Code:
#IBOutlet weak var marqueeText: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
UIView.animate(withDuration: 16.0, delay: 1, options: ([.curveLinear, .repeat]), animations: {() -> Void in
marqueeText.center = CGPoint(x: 0 -marqueeText.center.bounds.size.width / 2, y: text.center.y)
}, completion: { _ in })
}
Please check the below code :
#IBOutlet weak var marqueeText: UILabel!
var marqueeTextPoint = CGPoint()
var stopAnim: Bool = false
override func viewDidLoad() {
super.viewDidLoad()
marqueeTextPoint = marqueeText.center
marquee(text: marqueeText)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.marqueeText.center = self.marqueeTextPoint
marquee(text: marqueeText)
}
override func viewWillDisappear(_ animated: Bool) {
stopAnim = true
}
func marquee (text: UILabel) {
UIView.animate(withDuration: 16.0, delay: 1, options: ([.curveLinear, .repeat]), animations: {
text.center = CGPoint(x: 0 - text.bounds.size.width / 2, y: text.center.y)
}, completion: { _ in
if !(self.stopAnim) {
self.marquee(text: text)
} else {
text.layer.removeAllAnimations()
}
})
}
Title might be a bit ambiguous so i'll get straight to the point. Within my Xcode project I've implemented a simple animation in my initial ViewController, which functions fine, but after leaving the initial View, and moving to another scene, and then returning to the initial view, the animation fails to restart and I can't figure out why.
Here's the relevant code from my initial view
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//cloud animation
let oldCenter = cloud.center
let newCenter = CGPoint(x: oldCenter.x - 800, y: oldCenter.y)
UIView.animate(withDuration: 30.0, delay: 0.0, options:
[.curveLinear, .repeat], animations: {
self.cloud.center = newCenter
}, completion: nil)
//second cloud animation
let oldCenter2 = cloud2.center
let newCenter2 = CGPoint(x: oldCenter2.x + 800, y: oldCenter2.y)
UIView.animate(withDuration: 15.0, delay: 0.0, options:
[.curveLinear, .repeat], animations: {
self.cloud2.center = newCenter2
}, completion: nil)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
}
You should reset positions of the clouds to their original centers in viewDidDisappear:
var originalCenter: CGPoint!
var originalCenter2: CGPoint!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
originalCenter = cloud.center
originalCenter2 = cloud2.center
...
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
// restore positions
cloud.center = originalCenter
cloud2.center = originalCenter2
}
Inside a UIViewController, I call a .xib file and present it over the current UIView.
// initiate the pop up ad view and display it
let popUpAdView = PopUpAdViewController(nibName: "PopUpAdView", bundle: nil)
popUpAdView.displayIntoSuperView(view)
There is a button inside that .xib file that should remove itself from the screen when it's touched. However it doesn't perform so.
What exactly am I missing?
class PopUpAdViewController: UIViewController {
#IBOutlet weak var popUpAdView: UIView!
#IBOutlet weak var closeButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// adjust the view size from the device's screen size
let screenSize = UIScreen.mainScreen().bounds
view.frame = CGRectMake(0, 64, screenSize.width, screenSize.height-64)
// cosmetically adjust the view itself
view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.6)
view.userInteractionEnabled = true
closeButton.userInteractionEnabled = true
// style the pop up ad view
popUpAdView.layer.cornerRadius = 5
popUpAdView.layer.shadowOpacity = 0.8
popUpAdView.layer.shadowOffset = CGSizeMake(0.0, 0.0)
closeButton.addTarget(self, action: #selector(removeOutOfSuperView), forControlEvents: .TouchUpInside)
}
func displayIntoSuperView(superView: UIView!) {
superView.addSubview(self.view)
// define the initial cosmetical values of the items
popUpAdView.transform = CGAffineTransformMakeScale(0.3, 0.3)
popUpAdView.alpha = 0.0
closeButton.alpha = 0.0
// animate...
UIView.animateWithDuration(0.8, delay: 0.0, options: .CurveEaseIn, animations: {
self.popUpAdView.alpha = 1.0
self.popUpAdView.transform = CGAffineTransformMakeScale(1.0, 1.0)
}) { (Bool) in
UIView.animateWithDuration(0.6, delay: 1.5, options: .CurveEaseIn, animations: {
self.closeButton.alpha = 1.0
}, completion: { (Bool) in
})
}
}
func removeOutOfSuperView() {
UIView.animateWithDuration(0.5, delay: 0.0, options: .CurveEaseIn, animations: {
self.closeButton.alpha = 0.0
self.popUpAdView.transform = CGAffineTransformMakeScale(0.1, 0.1)
}) { (finished) in
UIView.animateWithDuration(0.8, delay: 0.0, options: .CurveEaseIn, animations: {
self.view.alpha = 0.0
self.view.removeFromSuperview()
}, completion: nil)
}
}
#IBAction func closePopUpAdView(sender: AnyObject) {
print("Closing the pop up ad...")
removeOutOfSuperView()
}
}
Update
.xib structureL: