Great, this works! Thank you! Can I set constraints programmatically, as I want to have the monkey in the upper left corner, or will there be problems too?
If youâre only doing simple things, then you shouldnât run into a problem.
You can always set constraints active or not active. You can also add your constraints into an array and set the entire array active or not.
Or instead of changing the center property, you could use the constraintâs constant property. (Although in a short test, I just added auto layout constraints in code, leaving the above method in place and it worked fine.)
Donât forget to set the viewâs translatesAutoresizingMaskIntoConstraints false if you are setting constraints in code.
Iâm really enjoying this tutorial thus far. Iâm using Xcode 7.3 with Swift 2.2 and am running into some problems getting the loadSound function to work. I changed it to use do/try/catch like this:
func loadSound(filename:NSString) -> AVAudioPlayer { let url = NSBundle.mainBundle().URLForResource(filename as String, withExtension: "caf") // Changed this from tutorial to work in Swift 2 do { let player = try AVAudioPlayer(contentsOfURL: url!) player.prepareToPlay() } catch { print("Error loading \(url)") } return player }
The problem is that the line âreturn playerâ gets the error âUse of unresolved identifier âplayerââ which I expect is because âplayerâ was created inside the do/try/catch statement. At this point Iâm not sure how to resolve this.
Hi Caroline,
Thank you so much for the tutorial, it really helped me to introduce to UIGestureRecognizer
Iâm trying to make a slight change but Iâm not able to resolve it myself Maybe you could give me a hint
Problem: when I move the monkey or banana, it goes over all the buttons and NavigationBar⊠the pan gesture has no limits
Solution I tried: I add the UIImageView (monkey/banana) to a UIView called âagregatedViewâ, where I thought the monkey would move only within the boundaries of this view.
monkey.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
agregatedView.addSubview(monkey)
monkey.userInteractionEnabled = true
let recognizer = UIPanGestureRecognizer(target: self, action:Selector(âhandlePan:â))
monkey.addGestureRecognizer(recognizer)
But NO! the monkey is added to agregatedView, and I can move it wherever I want.
Then, I tried to add the gesture to agregatedView itself, but of course, that moves the whole agregatedView.
what should I do in this case? any help would be really appreciated
Thank you again!!
@zakia - the monkey wonât stay within the boundaries, because you can position subviews outside of the main view. For example, you may want to animate a view from off-screen to on-screen. In that case, youâd give the subviewâs frame negative origin coordinates.
In your gesture handler, you can check the frame of the view that you are moving and check that it doesnât go outside the view.
In the gesture handler, you have this code:
if let view = recognizer.view {
let translation = recognizer.translationInView(self.view)
view.center = CGPoint(x:view.center.x + translation.x,
y:view.center.y + translation.y)
recognizer.setTranslation(CGPointZero, inView: self.view)
....
where you set the viewâs center with the new position. If the view wonât end up in the area that you want to keep it in, you donât have to set the new center.
Hi Caroline - great tutorial, thank you. In regards to the âGratuitous Decelerationâ section, I wanted to allow the images to be stopped mid-slide by a tap gesture i.e. another pan gesture, but the gesture was only recognised after the image had stopped moving. I included UIViewAnimationOptions.AllowUserInteraction in the animateWithDuration options, which improved sensitivity, but only when the image was moving slowly and not quickly (very bizarre).
I was also struggling with getting the images to stop moving if they hit the edge of the screen during a slide. Any boundaries set such as:
⊠were affected by finalPoint, where the image slowed down so that it reached the boundary at the end of its slide instead of maintaining its speed and being stopped mid-slide.
Iâm sure there are simple solutions, but perhaps you could advise me what to do here. Thanks once again
For some reason .allowUserInteraction only works at the very end of animation. Iâm absolutely baffled. I have tried using touchesMoved to intercept it the view, but I shouldnât really need to. Besides, it just created new problems.
Iâve tried putting UIViewAnimationOptions.AllowUserInteraction lines outside the .Ended if statement if thatâs what you mean? But shouldnât it work inside the statement since .allowUserInteraction is one of the animateWithDuration options? Doesnât work on device either
@timr - Iâve thought about it at length. I think if you want to do this, donât use gestures, but use touches began / moved / ended.
Hereâs the thing:
The animation should be done, as it is in the tutorial, when the user lets go (state = .Ended).
At this point, the pan gesture is still being used, so .AllowUserInteraction is irrelevant.
To stop the animation, you need some kind of external stopping. For example, this stops the animation (note no fancy stuff, just stopping monkeyView from animating)
@aiyub - it should be working. Did you go through the tutorial? When did it stop working?
The sample code has a custom tickle gesture implemented, but you can uncomment a return line to implement the pan gesture instead of the tickle gesture.