UITableViewCell setHighlighted:(BOOL)highlighted is always false - ios

I'm overriding UITableViewCell class to add a ripple/ink effect to my cells. Based on iOS Material Component List, the only thing I should do is to override the setHighlighted method like this:
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
if (highlighted) {
[self startInk];
} else {
[self endInk];
}
}
This technique works with UICollectionViewCell but I'm not sure if it should work with UITableViewCell.
Whenever I push the cell and I don't raise my finger, this method never gets called. Only gets called when and raise it and highlighted is always false.
Anyone knows how to override this long press to start an animation?
Thanking you in advance!

I finally get it by overriding (void)touchesBegan:withEvent: method. This is my code based on iOS Material Component List:
#property(strong, nonatomic, nonnull) MDCInkView *inkView;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
self.lastTouch = location;
if (self.selectionStyle != UITableViewCellSelectionStyleNone){
NSInteger selectionStyle = self.selectionStyle;
self.selectionStyle = UITableViewCellSelectionStyleNone;
[UIView animateWithDuration:0.25f
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
[self startInk];
}completion:^(BOOL finished) {
[UIView animateWithDuration:0.25f
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
[self endInk];
}completion:^(BOOL finished) {
self.selectionStyle = selectionStyle;
}];
}];
}
[super touchesBegan:touches withEvent:event];
}
- (void)startInk {
[self.inkView startTouchBeganAtPoint:_lastTouch animated:YES withCompletion:nil];
}
- (void)endInk {
[self.inkView startTouchEndAtPoint:_lastTouch animated:YES withCompletion:nil];
}

Related

How to create a scene like this: touch your screen, there will be a firstView, touch your screen again, the screen will show a second view in ios

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if (touch.tapCount == 1)
{
[UIView animateWithDuration:0.3 animations:^{
UIImage *image=[UIImage imageNamed:#"view1.png"];
imageView.image=image;
[self.view addSubview:imageView];
} completion:nil];
}
else if(touch.tapCount == 2)
{
[UIView animateWithDuration:0.3 animations:^{
UIImage *image=[UIImage imageNamed:#"view2.png"];
imageView.image=image;
[self.view addSubview:imageView];
} completion:nil];
}
}
this is my way, but the result is not what I want.
You have to double-click, it will show the second view, I also use tap gesture, but it's same, too.
Create Bool value global
#property(nonatomic) BOOL isFirstViewAdded
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if (!self.firstViewAdded)
{
self.firstViewAdded = true;
[UIView animateWithDuration:0.3 animations:^{
UIImage *image=[UIImage imageNamed:#"view1.png"];
imageView.image=image;
[self.view addSubview:imageView];
} completion:nil];
}
else if(self.firstViewAdded)
{
[UIView animateWithDuration:0.3 animations:^{
UIImage *image=[UIImage imageNamed:#"view2.png"];
imageView.image=image;
[self.view addSubview:imageView];
} completion:nil];
}
}
You can require tap gesture numberOfTapsRequired and numberOfTouchesRequired. In addition,
(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
will get any touch events, including UIButton touch events but tap gesture can't.
Use Tap gesture recogniser properly to handle single and double tap.
UITapGestureRecognizer *singleTapGesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapAction::)];
singleTapGesture.numberOfTapsRequired=1;
[self.view addGestureRecognizer:_singleTap];
UITapGestureRecognizer *doubleTapGesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doubleTapAction:)];
doubleTapGesture.numberOfTapsRequired = 2;
[singleTapGesture requireGestureRecognizerToFail:doubleTapGesture];
[self.view addGestureRecognizer:_singleTap];
- (void)singleTapAction:(UITapGestureRecognizer *)gestureRecognizer {
[UIView animateWithDuration:0.3 animations:^{
UIImage *image=[UIImage imageNamed:#"view1.png"];
imageView.image=image;
[self.view addSubview:imageView];
} completion:nil];
}
- (void)doubleTapAction:(UITapGestureRecognizer *)gestureRecognizer {
[UIView animateWithDuration:0.3 animations:^{
UIImage *image=[UIImage imageNamed:#"view2.png"];
imageView.image=image;
[self.view addSubview:imageView];
} completion:nil];
}
Hope this helps.

UITapGestureRecongnizer during animation failed

I am using the following code to generate an image which is touchable.
I read some posts here saying UIButton cannot be used during animation so I changed to use tap gesture. I have also added the UIViewAnimationOptionAllowUserInteraction in option. But I still cannot make it work. May I know the reason and any solution?
hit1 = [[UIImageView alloc]initWithFrame:CGRectMake(539,227,50,50)];
hit1.image=[UIImage imageNamed:#"roles.png"];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hitenermy:)];
[hit1 addGestureRecognizer:singleTap];
[hit1 setMultipleTouchEnabled:YES];
[hit1 setUserInteractionEnabled:YES];
hit1.userInteractionEnabled=YES;
[self.view addSubview:hit1];
[hit1 addGestureRecognizer:singleTap];
UIViewAnimationOptions options = UIViewAnimationCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction;
[UIView animateWithDuration:15.0
delay:1.0
options:options
animations:^{
hit1.center = CGPointMake(hit1.center.x-210, hit1.center.y-60);
}
completion:^(BOOL finished){
}];
You can use - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event to get touch event.
I have shown for a single imageView but if multiple imageView are present then u can use tag to differentiate them.
you can get more from :UIButton can't be touched while animated with UIView animateWithDuration
- (void)viewDidLoad
{
[super viewDidLoad];
hit1 = [[UIImageView alloc]initWithFrame:CGRectMake(539,227,50,50)];
hit1.image=[UIImage imageNamed:#"roles.png"];
[self.view addSubview:hit1];
UIViewAnimationOptions options = UIViewAnimationCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction;
[UIView animateWithDuration:15.0
delay:1.0
options:options
animations:^{
hit1.center = CGPointMake(hit1.center.x-210, hit1.center.y-60);
}
completion:^(BOOL finished){
}];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
for (UIImageView *imageView in self.view.subviews)
{
if ([imageView.layer.presentationLayer hitTest:touchLocation])
{
NSLog(#"Clicked");
break;
}
}
}

Xcode How to remove image after a number of touches?

I have touch event animations of 'clouds' using this code. All working fine but I want to fade/remove clouds after the user taps the cloud 3 times. So I want them to disappear after the third time it is touched. How do I do this?
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
CGRect cloudBLRect = [[[self.cloudBL layer] presentationLayer] frame];
if (CGRectContainsPoint(cloudBLRect, touchLocation)) {
NSLog(#"cloudBL tapped!");
cloudBLPressed = true;
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
self.cloudBL.center = CGPointMake(200, 600);
self.cloudBL.alpha = 0.5;
}
completion:^(BOOL finished) {
[UIView animateWithDuration:2.0
delay:2.0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
self.cloudBL.center = CGPointMake(100, 700);
self.cloudBL.alpha = 0.5;
} completion:^(BOOL finished) {
self.cloudBL.alpha = 1.0;
}];
} else {
NSLog(#"cloud not tapped.");
return;
}
if (cloudBLPressed) return;
}
Take a variable count and initialize it to 0. On each touch, increment it by 1. Also check in the touchGesture method, that if the count variable equals to 2 then set alpha for the cloud to 0.0.
Something like this:
in .m file take a private int vairiable: int count;
in viewDidLoad: count = 0; cloudView.alpha = 1.0;
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
count++;
if(count<2)
{
cloudView.alpha-=0.33;
}
else {
cloudView.alpha = 0.0;
}
}
Add it in your animation logic. Hope that helps.
You can set it in your code like this:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
CGRect cloudBLRect = [[[self.cloudBL layer] presentationLayer] frame];
if(count < 2)
{
count++;
if (CGRectContainsPoint(cloudBLRect, touchLocation)) {
NSLog(#"cloudBL tapped!");
cloudBLPressed = true;
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
self.cloudBL.center = CGPointMake(200, 200);
// self.cloudBL.alpha -=0.33;
}
completion:^(BOOL finished) {
[UIView animateWithDuration:2.0
delay:2.0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
self.cloudBL.center = CGPointMake(100, 300);
self.cloudBL.alpha -=0.33;
} completion:^(BOOL finished) {
// self.cloudBL.alpha = 1.0;
}];
}];
}
else {
NSLog(#"cloud not tapped.");
return;
}
if (cloudBLPressed) return;
} else {
[UIView animateWithDuration:2.0
delay:2.0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
self.cloudBL.center = CGPointMake(100, 300);
self.cloudBL.alpha =0.0;
} completion:^(BOOL finished) {
}];
}
}
You can do that:
UITapGestureRecognizer * tripleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(yourAction:)];
tripleTap.numberOfTapsRequired = 3;
[yourView addGestureRecognizer:tripleTap];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapOnImage:)];
[cloud addGestureRecognizer:tapRecognizer];
}
- (void)tapOnImage:(UITapGestureRecognizer *)gesture
{
tapsCounter++;
if (tapsCounter == 3)
{
// do your stuff
tapsCounter = 0;
}
}
I would suggest you to move this logic into UIImageView subclass that represents the cloud object.
Add the proper delegate UIGestureRecognizerDelegate to your interface.
Then in your viewDidLoad:
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapMethod)];
tapped.delegate=self;
tapped.numberOfTapsRequired = 3;
[self.view addGestureRecognizer:tapped];
and then in your View Controller:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if (touch.view != cloudView&& cloudView)
{
return YES;
}
return NO;
}
-(void)tapMethod
{
[cloudView removeFromSuperview];
cloudView = nil;
}

CGAffineTransformMakeScale animation on a cornerRadius rounded UIButton

I am rounding a UIBUtton, which is fine (self is a uibutton subclass):
self.layer.cornerRadius = self.frame.size.width/2;
self.layer.masksToBounds = YES;
self.clipsToBounds = YES;
But I am also trying to animate the button to shrink the scale then return to original size, like so:
[UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.layer.affineTransform = CGAffineTransformMakeScale(0.0f, 0.0f);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.layer.affineTransform = CGAffineTransformMakeScale(1.0f, 1.0f);
} completion:nil];
}];
The shrink/restore animation happens as it should. However, i lose the corner radius afterwards and the button goes back to a square. How can I animate and change the transform scale while maintaining the circular button?
I have also tried the following. It restores the button back to circular state at the end of the animation, but there is a fraction of a second where it boxes back to a square at the very beginning of the animation and it looks pretty bad:
[UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.layer.affineTransform = CGAffineTransformMakeScale(0.0f, 0.0f);
self.layer.cornerRadius = 0.0f;
self.layer.masksToBounds = YES;
self.clipsToBounds = YES;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.layer.affineTransform = CGAffineTransformMakeScale(1.0f, 1.0f);
self.layer.cornerRadius = self.frame.size.width/2;
self.layer.masksToBounds = YES;
self.clipsToBounds = YES;
} completion:nil];
}];
EDIT: I need the animation code and the rounding the button code, to all happen within the subclassed button. It needs to happen internally from the button class when the button is touched (ie, no code for rounding the button or calling the animation can be in the view controller).
EDIT WITH CLARIFIED SOLUTION:
I have no idea why this worked, but a portion of the below suggestion seemed to fix the problem. I was adding the animation method as an action on the custom button for UIControlEventTouchDown. I removed the control event action and instead called the animation method from -(void)touchesBegan.... inside the subclassed button class and everything seems to be working fine. Not sure why this happened. Would love further clarification/explanation, in a comment or something, if someone knows why this worked. Thanks again to #Shan for the help
Try by putting the animation part in controller class not in subclassed button class.
//in customButton.h file
#import <UIKit/UIKit.h>
#interface customButton : UIButton
- (id)initWithFrameOfCustomButton:(CGRect)frame;
- (void)animate;//this method u need to add and call it from the controller
#end
//in the subclassed UIButtin class
//in customButton.m file
#import "customButton.h"
#import <QuartzCore/QuartzCore.h>
#implementation customButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (id)initWithFrameOfCustomButton:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
//hear only set the frame only not incude animation part
//this is the custom initiliser that initilises the custom button with the given frame and also make it to round
self.frame = frame;
self.backgroundColor = [UIColor greenColor];
self.layer.cornerRadius = frame.size.width/2;
self.layer.masksToBounds = YES;
}
return self;
}
//add the animation part
- (void)animate //this method is called within the this class
{
[UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.layer.affineTransform = CGAffineTransformMakeScale(0.0f, 0.0f);
//edit: comment below lines becz u already made it to round
// self.layer.cornerRadius = 0.0f;
// self.layer.masksToBounds = YES;
// self.clipsToBounds = YES;
// CATransform3D t = CATransform3DIdentity;
// t = CATransform3DMakeScale(0 , 0, 1.0f);
// cButton.layer.transform = t;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.layer.affineTransform = CGAffineTransformMakeScale(1.0f, 1.0f);
// edit comment below lines
// self.layer.cornerRadius = self.frame.size.width/2;
// self.layer.masksToBounds = YES;
// self.clipsToBounds = YES;
// CATransform3D t = CATransform3DIdentity;
// t = CATransform3DMakeScale(1 , 1, 1.0f);
// cButton.layer.transform = t;
} completion:nil];
}];
}
//implement touch handling methods to call animation
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self animate];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self animate];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// [self animate];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// [self animate];
}
in the controller class include the animation part like below
//in the view controller that u are using this custom button
#import "FirstViewController.h"
#import "CustomCell.h"
#import "customButton.h"
#import <QuartzCore/QuartzCore.h>
#interface FirstViewController ()
#end
#implementation FirstViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
customButton *cButton = [[customButton alloc]initWithFrameOfCustomButton:CGRectMake(20, 30, 100, 100)]; //hear i am initilising the custom button
cButton.tag = 100;//assigning tag to access during the animation
[self.view addSubview:cButton];
// [self makeAnimation];//i am animation rite after it loads the all views u can place this method call where ever u want
//edit: i am commenting the this line so animations of the button in this class won't call
}
//edit below method is added
- (void)viewDidAppear:(BOOL)animated
{
customButton *cButton = (customButton *)[self.view viewWithTag:100];
//edit->comment this line so that u never call the animation from view controller
// [cButton animate];//animating the button the code in the subclassed method is called
}
- (void)makeAnimation
{
customButton *cButton = (customButton *)[self.view viewWithTag:100];//get the custom button by using the tag
[UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
cButton.layer.affineTransform = CGAffineTransformMakeScale(0.0f, 0.0f);
// cButton.layer.cornerRadius = 0.0f;
// cButton.layer.masksToBounds = YES;
// cButton.clipsToBounds = YES;
// CATransform3D t = CATransform3DIdentity;
// t = CATransform3DMakeScale(0 , 0, 1.0f);
// cButton.layer.transform = t;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
cButton.layer.affineTransform = CGAffineTransformMakeScale(1.0f, 1.0f);
// cButton.layer.cornerRadius = cButton.frame.size.width/2;
// cButton.layer.masksToBounds = YES;
// cButton.clipsToBounds = YES;
// CATransform3D t = CATransform3DIdentity;
// t = CATransform3DMakeScale(1 , 1, 1.0f);
// cButton.layer.transform = t;
} completion:nil];
}];
}
note UIButton inherits from: UIControl -> UIView -> UIResponder -> NSObject
see docs if u are confused
if you want use UIControlers i.e responder then u need to implement these methods
you can get the event for which u want,
in your subclassed UIButton class implement this by commenting touch handling methods
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
return YES;
}
- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
}
- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
return YES;
}
Similar to Shankar BS answer, but with swift 3.
Transformation with bounced animation:
myButton.layer.setAffineTransform(CGAffineTransform(scaleX: 0.0, y: 0.0))
UIView.animate(withDuration: 0.7, delay: 1.0,
usingSpringWithDamping: 0.7, initialSpringVelocity: 7.0, options: [],
animations: {
myButton.layer.setAffineTransform(CGAffineTransform(scaleX: 1.0, y: 1.0))
},
completion: nil
)

Stop and start an animation by touch. Objective C

I have made an animation that moves across the screen, my animation loops continuously. How can I stop the animation when you tap the animated image, Then let the animation continue when you lift the touch?
I know how to use TouchesMoved to move a specified button like this:
CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
UIControl *control = sender;
control.center = point;
But getting it to work with my animation. I would like the animation to continue after I touch it.
SelectedCellViewController.h
// SelectedCellViewController.h
#import <Accounts/Accounts.h>
#import <QuartzCore/QuartzCore.h>
#interface SelectedCellViewController : UIViewController {
IBOutlet UIImageView *imageView;
UIImageView *rocket;
}
#end
viewControllertoShow.m
#import "SelectedCellViewController.h"
#interface SelectedCellViewController ()
#end
#implementation SelectedCellViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
}
return self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
[self performSelector:#selector(imageSpawn:) withObject:nil afterDelay:3];
}
- (void) imageSpawn:(id) sender
{
UIImage* image = [UIImage imageNamed:#"ae"];
rocket = [[UIImageView alloc] initWithImage:image];
rocket.frame = CGRectMake(-25, 200, 25, 40);
[UIView animateWithDuration:5
delay:0.2f
options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
animations:^(){rocket.frame=CGRectMake(345, 200, 25, 40);}
completion:^(BOOL fin) {
}];
[self.view addSubview:rocket];
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(ballTapped:)];
tapped.numberOfTapsRequired = 1;
[rocket addGestureRecognizer:tapped];
[rocket setUserInteractionEnabled:YES];
}
-(void)ballTapped:(UIGestureRecognizer *)gesture
{
CGPoint location = [gesture locationInView:gesture.view];
//then write code to remove the animation
[self.view.layer removeAllAnimations];
NSLog(#"Tag = %d", gesture.view.tag);
rocket.frame = CGRectMake(location.x,location.y,25,40);
}
- (void)dismissView {
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)viewDidUnload {
}
#end
As you had already stated in your question ,you can get the touched point using
CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
Then check check whether this point lies inside the coordinates of the animated UIImageview and then stop animation.
But if you are using scrollview, you won't be able to use this because scrollview will not return any UIView touch events.
As your image view is animating, a better choice will be adding a UITapGestureRecogniser to the image view when you add it s subview, like this
- (void) imageSpawn:(id) sender
{
UIImage* image = [UIImage imageNamed:#"ae"];
UIImageView *rocket = [[UIImageView alloc] initWithImage:image];
rocket.frame = CGRectMake(-25, 200, 25, 40);
[UIView animateWithDuration:5
delay:0.2f
options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
animations:^(){rocket.frame=CGRectMake(345, 200, 25, 40);}
completion:^(BOOL fin) {
}];
[myScrollView addSubview:rocket];
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(ballTapped:)];
tapped.numberOfTapsRequired = 1;
[self.rocket addGestureRecognizer:tapped];
[rocket setUserInteractionEnabled:YES];
}
And in the target function write code to stop the animation:
-(void)ballTapped:(UIGestureRecognizer *)gesture
{
//here also you can get the tapped point if you need
CGPoint location = [gesture locationInView:gesture.view];
//then write code to remove the animation
[self.view.layer removeAllAnimations];
}
EDIT:
If you are trying to stop the image view at the touched point, you can add this to ballTapped event:
rocket.frame = CGRectMake(location.x,location.y,25,40);
But for this you have to declare UIImageView *rocket outside this particular method, i.e. declare to in your header file.
Another way is to add this to the parent view of the animation -
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
self.animating = NO;
return [super hitTest:point withEvent:event];
}
use an (atomic) property, then check the property in the animation to see if it should be stopped. We use this to stop a photo gallery which is running so that the user can manually move the photos. You could also check in here if the point is in a specific area if necessary. This method runs before any touch methods are called.
try like this may be it helps you, in the middle of the animation if you touch on the imageview then animation removes from the view .
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint lastLocation = [touch locationInView: self.view];
if([[touch view] isKindOfClass:[UIImageView class]]){
[youImageView.layer removeAllAnimations];
youImageView.center=lastLocation;//assign your image view center when the animation removes from the view.
}
}
and add #import <QuartzCore/QuartzCore.h> framework.

Resources