Tutorial details

Playing around with Core Graphics, Core Animation and Touch Events  | App Code for Sale | Preview

Playing around with Core Graphics, Core Animation and Touch Events - (Part 2) | iOS Tutorial

Add some behavior to images

Overview PAGE TOP

Hi Guys! Welcome back to the last part of this tutorial.

In the previous post I showed you how to draw an image using Core Graphics, in this post we’ll add some behavior to that image.

Handling touch

We can start by updating the circle position depending on the touch location. Here I’ll briefly show you how to use the UIPanGestureRecognizer to get all the needed data.

ViewController.m - viewDidLoad  
1   //Initialize Gesture recognizer ----------------
2   UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(screenPan:)];
3   [self.view addGestureRecognizer:panGestureRecognizer];

Essentially we initialize a Pan Gesture Recognizer and we attach it to the ViewController view at the end of the viewDidLoad function. We initialize it using the target-action pattern, using the ViewController itself as target and a function called screenPan as action. The method ScreenPan will be executed as soon as a Pan Gesture is recognized on the ViewController view.

Let’s see how the method screenPan is implemented:

1   //Manage the Pan Gesture
2   - (void)screenPan:(UIGestureRecognizer *)gesture{
3        
4       //Get the location of the touch
5       CGPoint point = [gesture locationInView:self.view];
6        
7       //Move the center of the gradient view if gesture state is equal to Began or Changed
8       if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
9           self.gradientView.center = point;
10      }
11  }

This method is based on 2 steps. First we obtain the location of the touch thanks to the method locationInView: and then, if the state of the gesture is equal to “began” or “changed”, we assign it to the property center of gradientView.

As you can see this code works only while dragging but not at the first touch.

To make it work we need to implement some touch event handling methods, like touchesBegan and touchesEnded which will be fired automatically in response to user actions (you can find more information about these methods in the documentation).

The touchesBegan method does something really similar to what the screenPan method does. It just changes the current gradientView center to the touch location.

1   //Manage Touches Begin
2   -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
3            
4       UITouch *touch = [touches anyObject];
5       CGPoint drawPoint = [touch locationInView:self.view];
6        
7       //Change gradientView center and launch fadeIn animation
8       self.gradientView.center = drawPoint;
9   }

Thanks to this function when the user touches the screen, the gradientView moves immediately to the new position.

Adding animations

To make this experiment more interesting we can animate the opacity of the red circle just to make it appear slowly when the user moves it and to make it fade out when the touch action ends.

The animations are generated through Core Animation using the class CABasicAnimation. This is probably the simplest way to animate a property, in fact it’s just going to take some simple steps. Let’s see the code needed to create the fadeIn animation:

GradientCircle.m    
1   //Create the Fadein animation and assign it to the layer
2   -(void)fadeIn{
3   
4       //Define the property through a Key
5       CABasicAnimation *fadein = [CABasicAnimation animationWithKeyPath:@"opacity"];
6   
7       //Initial and the final values
8       fadein.fromValue = [NSNumber numberWithInt:0];
9       fadein.toValue = [NSNumber numberWithInt:1];
10  
11      //Duration the the animation
12      fadein.duration = 0.3;
13       
14      //Attach the animation to the layer for a custom key... let's call it "fade"
15      [self.layer addAnimation:fadein forKey:@"fade"];
16  }

Firstly we define the CABasicAnimation object specifying the key path of the property to be animated, opacity.

The next step is to define how the animation takes place. So with the help of the properties fromValue, toValue and duration we say that in 0.3 seconds the opacity must change from 0 to 1. The animation between the initial and the final value is automatically created for us thanks to an interpolation calculated over the duration. So we don’t need to take care of that because it just works great producing a really smooth animation :) .

We have now defined the animation and we can attach it to the layer of the current GradientCircle object using the function addAnimation:forKey:. This function adds the animation to the render tree specifying a unique key to refer to this animation (this is not required). When this function is called, the animation starts.

The fadeOut function is really similar to the fadeIn function. We just invert the fromValue and the toValue to make the object disappears.

GradientCircle.m    
1   //Create the Fadeout animation, assign it to the layer 
2   -(void)fadeOut{
3        
4       CABasicAnimation *fadeout = [CABasicAnimation animationWithKeyPath:@"opacity"];
5       fadeout.delegate = self;
6       fadeout.fromValue = [NSNumber numberWithInt:1.0];
7       fadeout.toValue = [NSNumber numberWithInt:0];
8       fadeout.duration = 0.2;
9        
10      [self.layer addAnimation:fadeout forKey:@"fade"];
11  }

Remember to add the function prototypes to the GradientCircle.h file.

We can now call the fadeOut function in relation to the touch actions that we have previously managed. When the user touches the screen we make the object appear, while when the user stops touching the object has to disappear.

This is extremely simple, just call the animation functions (fadeIn/fadeOut) in the right places.

Add the fadeIn function at the end of the function touchesBegan:

1   [self.gradientView fadeIn];

And add the touchesEnded function to manage the end of the touch operations (this function is automatically called when a touch operation ends):

1   //Manage Touches End
2   -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
3       [self.gradientView fadeOut];
4   }

Last, we want to call the function fadeOut when the Pan Gesture ends. So add this code at the end of the screenPan function:

1   //Launch fadeOut animation if gesture state is equal to end
2   if (gesture.state == UIGestureRecognizerStateEnded) {
3       [self.gradientView fadeOut];
4   }

We are almost done…

If we execute the code now, though, we’ll encounter a problem. When the fadeOut animation ends the GradientCircle object appears again. To prevent it from happening we have to add 2 rows of code to the fadeOut function.

1   fadeout.fillMode = kCAFillModeForwards;
2   fadeout.removedOnCompletion = NO;

Setting fillMode to the kCAFillModeForwards constant we tell the animation to stick its value to the final value.

The removedOnCompletion tells the animation not to remove itself from the layer tree. Compile and execute to see the final result!

Compile and execute to see the final result!

We’re done for this tutorial. If you haven’t done it yet, feel free to download the source code and play around with it.

Reference PAGE TOP

http://www.thinkandbuild.it/playing-around-with-core-graphics-core-animation-and-touch-events-part-2/

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.