cs193p – Lecture #11 Unwind Segues, Alerts, Timers, View Animation

Eadweard Muybridge [Public domain oder Public domain], via Wikimedia Commons

Please note, this blog entry is from a previous course. You might want to check out the current one.

The eleventh lecture is theory only, starting with detailed discussion about the final project (which might only be interesting for Stanford students).

Unwind Segue

Unwind Segues provide a way to segue back to MVCs which directly or indirectly presented the current segue.

Control dragging to the exit button of the current MVC you can choose all methods of parent (presenting) MVCs marked with @IBAction. When the segue is triggered it gets prepared, dismisses the current MVC and called the named method.

Alerts and Action Sheets

Alerts pop up in the middle of the screen and ask questions with a small number of choices. Because they are disruptive to the user, they should be used carefully, e.g. only for problems.

var alert = UIAlertController(
    title: “Login Required”,
    message: “Please enter your Cassini guidance system...”,
    preferredStyle: UIAlertControllerStyle.Alert
)
alert.addAction(UIAlertAction(
    title: “Cancel”,
    style: .Cancel)
    { (action: UIAlertAction) -> Void in
        // do nothing
    }
)
alert.addAction(UIAlertAction(
    title: “Login”,
    style: .Default)
    { (action: UIAlertAction) -> Void in
        // get password and log in
        let tf = self.alert.textFields?.first as? UITextField
        if tf != nil { self.loginWithPassword(tf.text) }
    }
)
alert.addTextFieldWithConfigurationHandler { (textField) in
    textField.placeholder = “Guidance System Password”
}
presentViewController(alert, animated: true, completion: nil)

Action sheets slide up from the bottom of the screen or as pop over and ask questions with more answers.

var alert = UIAlertController(
    title: "Redeploy Cassini",
    message: "Issue commands to Cassini’s guidance system.",
    preferredStyle: UIAlertControllerStyle.ActionSheet
)
alert.addAction(UIAlertAction(
    title: “Orbit Saturn”,
    style: UIAlertActionStyle.Default)
    { (action: UIAlertAction) -> Void in
        // go into orbit around saturn
    }
)
alert.addAction(UIAlertAction(
    title: “Explore Titan”,
    style: .Default)
    { (action: UIAlertAction) -> Void in
        if !self.loggedIn { self.login() }
        // if loggedIn go to titan
    }
)
alert.addAction(UIAlertAction(
    title: “Closeup of Sun”,
    style: .Destructive)
    { (action: UIAlertAction) -> Void in
        if !loggedIn { self.login() }
        // if loggedIn destroy Cassini by going to Sun
    }
)
alert.addAction(UIAlertAction(
    title: “Cancel”,
    style: .Cancel)
    { (action: UIAlertAction) -> Void in
       // do nothing
    }
)
alert.modalPresentationStyle = .Popover
et ppc = alert.popoverPresentationController
ppc?.barButtonItem = redeployBarButtonItem
presentViewController(alert, animated: true, completion: nil)

NSTimer

Timers can be used to start future tasks once or repeatedly in the future, without promise of “exact” timing.

let timer = NSTimer.scheduledTimerWithTimeInterval(2.0
    target: self, selector: “fire:”, 
    userInfo: nil,
    repeats: true
)
func fire(timer: NSTimer) {
    if iAmDoneWithThisTimer {
        timer.invalidate()
    } 
}

Tolerances may be set for late firing …

UIView Animation

frame, transform (translation, rotation and scale) and alpha of view can be animated.

if myView.alpha = 1.0 {
    UIView.animateWithDuration(3.0
                        delay: 2.0
                      options: UIViewAnimationOptions.CurveEaseInEaseOut
                   animations: { myView.alpha = 0.0 }
                   completion: { if $0 { myView.removeFromSuperview() } })
    println(“myView.alpha = \(myView.alpha)”)
 }

During the animation the view has already its target properties.

Possible UIViewAnimationOptions are:

BeginFromCurrentState
AllowUserInteraction
LayoutSubviews
Repeat
Autoreverse
OverrideInheritedDuration
OverrideInheritedCurve
AllowAnimatedContent
CurveEaseInEaseOut
CurveEaseIn
CurveLinear

It is also possible to flip an view over (UIViewAnimationOptionsTransitionFlipFrom{Left,Right,Top,Bottom}), to dissolve between two states (UIViewAnimationOptionsTransitionCrossDissolve), and curling up or down (UIViewAnimationOptionsTransitionCurl{Up,Down})

UIView.transitionWithView(view: myPlayingCardView,
                      duration: 0.75,
                       options: UIViewAnimationOptions.TransitionFlipFromLeft,
                    animations: { cardIsFaceUp = !cardIsFaceUp }
                    completion: nil)

The lecture and its slides are available via iTunes named “11. Unwind Segues, Alerts, Timers, View Animation”.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

Leave a Reply

Your email address will not be published.