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;
}
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];
}
- (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 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;
}
}
}
I have to find Endpoint from start and moved point.
I am doing animation and I need to move View when user drag the view then I have to through it out of the screen and bring back to the original point.
Right now I have used UISwipeGestureRecognizer for detecting swipe on Move. Following is the code.
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
// Setting the swipe direction.
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[swipeUp setDirection:UISwipeGestureRecognizerDirectionUp];
[swipeDown setDirection:UISwipeGestureRecognizerDirectionDown];
// Adding the swipe gesture on image view
[_view1 addGestureRecognizer:swipeLeft];
[_view1 addGestureRecognizer:swipeRight];
[_view1 addGestureRecognizer:swipeUp];
[_view1 addGestureRecognizer:swipeDown];
Handling Swipe
- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {
CGPoint movedPoint = [swipe locationInView:swipe.view];
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(#"Left Swipe");
CGPoint startPoint = _view1.frame.origin;
//Diffence Moved
float movedDiffence_X = startPoint.x - movedPoint.x;
float movedDiffence_Y = startPoint.y - movedPoint.y;
//How can I find END POINT BASED ON THIS DATA
[UIView animateWithDuration:1 animations:^{
_view1.center = CGPointMake(movedDiffence_X *3,movedDiffence_Y *3 );
_view1.transform = CGAffineTransformMakeRotation(-0.86);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.8 animations:^{
_view1.center = CGPointMake(84, 240);
_view1.transform = CGAffineTransformMakeRotation(0.36);
} completion:^(BOOL finished) {
}];
}];
}
if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(#"Right Swipe");
CGPoint startPoint = _view1.frame.origin;
//Diffence Moved
float movedDiffence_X = startPoint.x - movedPoint.x;
float movedDiffence_Y = startPoint.y - movedPoint.y;
//How can I find
[UIView animateWithDuration:1 animations:^{
_view1.center = CGPointMake(movedDiffence_X *3,movedDiffence_Y *3 );
_view1.transform = CGAffineTransformMakeRotation(-0.86);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.8 animations:^{
_view1.center = CGPointMake(84, 240);
_view1.transform = CGAffineTransformMakeRotation(0.36);
} completion:^(BOOL finished) {
}];
}];
}
if (swipe.direction == UISwipeGestureRecognizerDirectionUp) {
NSLog(#"Up Swipe");
CGPoint startPoint = _view1.frame.origin;
//Diffence Moved
float movedDiffence_X = startPoint.x - movedPoint.x;
float movedDiffence_Y = startPoint.y - movedPoint.y;
//How can I find
[UIView animateWithDuration:1 animations:^{
_view1.center = CGPointMake(movedDiffence_X *3,movedDiffence_Y *3 );
_view1.transform = CGAffineTransformMakeRotation(-0.86);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.8 animations:^{
_view1.center = CGPointMake(84, 240);
_view1.transform = CGAffineTransformMakeRotation(0.36);
} completion:^(BOOL finished) {
}];
}];
}
if (swipe.direction == UISwipeGestureRecognizerDirectionDown) {
NSLog(#"Down Swipe");
CGPoint startPoint = _view1.frame.origin;
//Diffence Moved
float movedDiffence_X = startPoint.x - movedPoint.x;
float movedDiffence_Y = startPoint.y - movedPoint.y;
//How can I find
[UIView animateWithDuration:1 animations:^{
_view1.center = CGPointMake(movedDiffence_X *3,movedDiffence_Y *3 );
_view1.transform = CGAffineTransformMakeRotation(-0.86);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.8 animations:^{
_view1.center = CGPointMake(84, 240);
_view1.transform = CGAffineTransformMakeRotation(0.36);
} completion:^(BOOL finished) {
}];
}];
}
}
When I swipe No.1 view then I can get moved point in SwipeHandler (handleSwipe) Method
So I can also detect Direction of Swipe. But my problem is that I have to through No.1 View out of the screen. For that I have to find the Endpoint.
So, how can I find Endpoint from Starting point and MovedPoint ?
Here is the link fro the endpoint calculation. http://library.thinkquest.org/20991/geo/coordgeo.html
I need to handle touch on my object when it move across my screen. When touchesBegan called I need to hide my object.
this is my code in UIViewController:
- (void)win {
for (NSInteger i = 1; i <= 6; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"rocket_%d.png", i]]];
imageView.tag = 1000;
CGSize size = imageView.image.size;
[imageView setFrame:CGRectMake(200 + (i * 60), 500, size.width, size.height)];
[self.view addSubview:imageView];
[rockets addObject:imageView];
[imageView setUserInteractionEnabled:YES];
}
[self startRockets];
}
- (void)startRockets {
CGFloat timeInterval = 1.0;
for (UIImageView *imageView in rockets) {
[UIView animateWithDuration:5.0 delay:timeInterval options:UIViewAnimationOptionAllowUserInteraction animations:^{
[imageView setFrame:CGRectMake(imageView.frame.origin.x, 0, imageView.frame.size.width, imageView.frame.size.height)];
} completion:nil];
timeInterval += 1.0;
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
NSArray *views = [self.view subviews];
for (UIView *v in views) {
if (v.tag == 1000) {
if (CGRectContainsPoint(v.frame, touchLocation) == YES) {
[v setHidden:YES];
}
}
}
}
Maybe you are missing the AllowAnimatedContent. Try changing your
UIViewAnimationOptionAllowUserInteraction
to
UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionAllowAnimatedContent
...another way might be to use UITapGestureRecognizer :
- (void)createViews {
rockets = [[NSMutableArray alloc] init];
for (NSInteger i = 1; i <= 6; i++) {
UIView *view = [UIView new];
view.backgroundColor = [UIColor redColor];
view.frame = CGRectMake((i * 60), 100, 50, 50);
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(rocketTouched:)];
[view addGestureRecognizer:tap];
[self.view addSubview:view];
[rockets addObject:view];
}
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(moveViews) userInfo:nil repeats:YES];
}
- (void)moveViews {
for (UIView * view in rockets) {
[UIView animateWithDuration:5.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionAllowAnimatedContent animations:^{
view.frame = CGRectMake(view.frame.origin.x -10, 0, view.frame.size.width, view.frame.size.height);
} completion:nil];
}
}
- (void)rocketTouched:(UITapGestureRecognizer*)tap {
tap.view.hidden = YES;
}
You need also check this if you need to detect intersection with object.
I have edited touchesBegan method and it works.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
for (UIImageView *imageView in rockets) {
CALayer *bbl1ObjPresentationLayer = (CALayer*)[imageView.layer presentationLayer];
if(CGRectIntersectsRect(bbl1ObjPresentationLayer.frame, CGRectMake(touchLocation.x, touchLocation.y, 1, 1))) {
NSLog(#"intersect");
//They are intersecting
}
}
}
if you do it using CALayer insted of UIImageView object this work correct. Check this answer link