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;
}
}
}
Related
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];
}
This is the screen I am working on currently. As you can see below side an UIView where I have used label and four buttons. And below that I have used table view to show menu. I have used SwipeGesture in order to slide the view up and down. What I want now whenever I touch the background image the UIView slides down completely and when I remove my finger
from the screen the UIView automatically bounce up to the same position as shown in the image. Can anyone help me out??
This is my code I have worked so far-
UISwipeGestureRecognizer *gestureRecognizerUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeHandlerUp:)];
[gestureRecognizerUp setDirection:(UISwipeGestureRecognizerDirectionUp)];
[self.subview addGestureRecognizer:gestureRecognizerUp];
UISwipeGestureRecognizer *gestureRecognizerDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeHandleDown:)];
[gestureRecognizerDown setDirection:(UISwipeGestureRecognizerDirectionDown)];
[self.subview addGestureRecognizer:gestureRecognizerDown];
- (void)swipeHandlerUp:(UISwipeGestureRecognizer *)sender {
if (sender.direction == UISwipeGestureRecognizerDirectionUp){
[UIView animateWithDuration:0.3 animations:^{
CGPoint Position = CGPointMake(self.view.frame.origin.x, self.view.frame.origin.y+20);
self.subview.frame = CGRectMake(Position.x , Position.y , self.subview.frame.size.width, self.subview.frame.size.height);
[self.navigationController popViewControllerAnimated:YES];
}];
}
}
- (void)swipeHandleDown:(UISwipeGestureRecognizer *)sender {
if (sender.direction == UISwipeGestureRecognizerDirectionDown){
[UIView animateWithDuration:0.3 animations:^{
CGPoint Position = CGPointMake(self.view.frame.origin.x , self.view.frame.origin.y+430);
self.subview.frame = CGRectMake(Position.x , Position.y , self.subview.frame.size.width, self.subview.frame.size.height);
[self.navigationController popViewControllerAnimated:YES];
}];
}
}
You can do this in -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;
And make sure to deselect all the three gesture behaviours like for both your swipeGestures.
Your_Swipe_gesture_Object.delaysTouchesBegan = NO;
Your_Swipe_gesture_Object.delaysTouchesEnded = NO;
Your_Swipe_gesture_Object.cancelsTouchesInView = NO;
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
[UIView animateWithDuration:0.3 animations:^{
CGPoint Position = CGPointMake(self.view.frame.origin.x, self.view.frame.origin.y+20);
self.subview.frame = CGRectMake(Position.x , Position.y , self.subview.frame.size.width, self.subview.frame.size.height);
[self.navigationController popViewControllerAnimated:YES];
}];
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
[UIView animateWithDuration:0.3 animations:^{
CGPoint Position = CGPointMake(self.view.frame.origin.x , self.view.frame.origin.y+430);
self.subview.frame = CGRectMake(Position.x , Position.y , self.subview.frame.size.width, self.subview.frame.size.height);
[self.navigationController popViewControllerAnimated:YES];
}];
}
- (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.
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;
}
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.