Basic UIView Animation with Swift Tutorial

Discussion of the official tutorials published on raywenderlich.com. Please only discuss the official tutorials here - for general questions, use the General Discussion forum instead.

Basic UIView Animation with Swift Tutorial

Postby rwenderlich » Thu Aug 07, 2014 9:00 am

This is the official thread to discuss the following blog post: Basic UIView Animation with Swift Tutorial
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ray Wenderlich
Blog: http://www.raywenderlich.com
Twitter: http://twitter.com/rwenderlich
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
User avatar
rwenderlich
Site Admin
 
Posts: 2512
Joined: Thu Dec 23, 2010 4:14 pm
Has thanked: 36 times
Been thanked: 479 times

Re: Basic UIView Animation with Swift Tutorial

Postby dojomaroc » Thu Aug 07, 2014 8:58 pm

The messages "Bug not tapped!" and "Bug tapped!" are still logged to the console even after removing the bug from its superview, so I would suggest the following

- Declare 'tap' as global variable with a default value

Code: Select all
var tap = UITapGestureRecognizer(target: nil, action: Selector())


- At the end of viewDidAppear()

Code: Select all
tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))


- When the bug is tapped remove the gesture recognizer

Code: Select all
func handleTap(gesture: UITapGestureRecognizer) {
    let tapLocation = gesture.locationInView(bug.superview)
    if bug.layer.presentationLayer().frame.contains(tapLocation) {
         view.removeGestureRecognizer(tap)
         println("Bug tapped!")
    } else {
         println("Bug not tapped!")
    }
}
Last edited by dojomaroc on Fri Sep 19, 2014 4:35 pm, edited 1 time in total.
dojomaroc
Hacker
 
Posts: 12
Joined: Mon May 12, 2014 8:54 pm
Has thanked: 0 time
Been thanked: 0 time

Postby skourdes » Fri Aug 15, 2014 1:35 am

I am using Xcode 6 Beta 5 and I am always receiving an error on the line:

let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))

It says Extra argument 'target' in call.

Does anybody else get the same error when trying to compile?
skourdes
n00b
 
Posts: 2
Joined: Thu Dec 26, 2013 12:37 am
Has thanked: 0 time
Been thanked: 0 time

Re: Basic UIView Animation with Swift Tutorial

Postby skourdes » Fri Aug 15, 2014 2:04 am

Actually, what I did was change the simulator to iPhone 5 from iPhone 5S and the error disappeared after re-compiling.
It seems it only occurs when choosing the iPhone 5S simulator
skourdes
n00b
 
Posts: 2
Joined: Thu Dec 26, 2013 12:37 am
Has thanked: 0 time
Been thanked: 0 time

Postby Tokomon » Sat Aug 16, 2014 7:56 am

how to implement this animation when I want to use Autolayout?
Tokomon
Hacker
 
Posts: 15
Joined: Mon Dec 16, 2013 3:45 pm
Has thanked: 1 time
Been thanked: 1 time

Postby naomipunkclan » Tue Aug 19, 2014 5:56 pm

After the bug has been squashed an actual Swift bug may be apparent... The bug is removed from superview. Now click on the screen and see that there is a runtime access of an unwrapped optional which is now nil.
If you did remove the gesture recognizer as suggested above by Dojomaroc - you will not reach this bug. Alternatively it is an opportunity to learn more how to handle optionals in Swift: (note the ? in optional chaining will unwrap an optional if it is not nil)

Code: Select all
  func handleTap(gesture: UITapGestureRecognizer) {
        let tapLocation = gesture.locationInView(bug?.superview)
        if let bugTapped = bug?.layer.presentationLayer().frame?.contains(tapLocation) {
            if bugTapped {
                println("Bug tapped!")
                // add bug-squashing code here
                if isBugDead { return }
                isBugDead = true
                UIView.animateWithDuration(0.7, delay: 0.0, options: .CurveEaseOut, animations: {
                    self.bug.transform = CGAffineTransformMakeScale(1.25, 0.75)
                    }, completion: { finished in
                        UIView.animateWithDuration(2.0, delay: 2.0, options: nil, animations: {
                            self.bug.alpha = 0.0
                            }, completion: { finished in
                                self.bug.removeFromSuperview()
                        })
                    })
            }
             else {
            println("Bug not tapped!")
            }
        }
        else {
            println("Bug does not exist!")           
        }
    }



So the gesture recognizer is still active, but the bug is found not to exist! :)
naomipunkclan
n00b
 
Posts: 2
Joined: Wed Apr 20, 2011 4:12 pm
Has thanked: 0 time
Been thanked: 0 time

Postby GeoffCoope » Thu Nov 13, 2014 2:30 pm

Would love to see this tutorial modified for OSX
GeoffCoope
Hacker
 
Posts: 18
Joined: Thu Nov 03, 2011 10:53 am
Has thanked: 0 time
Been thanked: 0 time

Re: Basic UIView Animation with Swift Tutorial

Postby BjornRuud » Sun Dec 14, 2014 1:15 pm

Better late than never. Project has been updated for Xcode 6.1.1, and gesture recognizer bug has been fixed.
User avatar
BjornRuud
Baby Hacker
 
Posts: 6
Joined: Thu May 22, 2014 11:01 am
Has thanked: 0 time
Been thanked: 0 time

Postby vani2 » Thu May 14, 2015 7:35 am

Isn't a using strong self in animation blocks a bad practice? I guess "weakify" self is right solution.
vani2
n00b
 
Posts: 1
Joined: Mon Feb 03, 2014 5:35 pm
Has thanked: 0 time
Been thanked: 0 time

Postby DataHog » Wed Dec 30, 2015 4:01 pm

I am using XCode 7.1 and Swift2 and am having an issue with UIView.animateWithDuration options because we are trying to combine two options by or'ing them together but under Swift2 they are different types: .CurveEaseInOut is an enum and .AllowUserInteraction is a struct. here is the code:
UIView.animateWithDuration(1.0, delay: 2.0, options:.CurveEaseInOut | .AllowUserInteraction, animations: { () -> Void in
self.bug.center = CGPoint(x: 75, y: 200)
}) { (finished) -> Void in
print("Bug moved left")
self.faceBugRight()
}
}

the error:
"type of expression is ambiguous without more context".

Is it possible to effectively pre-process the struct reference and somehow combine them or do I have to write a stub in objective c to do this? If so, I view it as a patch.
DataHog
n00b
 
Posts: 1
Joined: Wed Dec 30, 2015 3:47 pm
Has thanked: 0 time
Been thanked: 0 time

Next

Return to Official Tutorials

Who is online

Users browsing this forum: Baidu [Spider] and 2 guests