animateWithDuration and touchesBegan don't work in pair - ios

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

Related

Create dynamic uiview on double tap

Hello fellow programmers.,
I want to create dynamic UIView of size 100X100 around my double click on Main UIView (Everytime I double click diffrant place). I totally don't have any idea about it. So can't provide any code for it. Anyone can help??
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch = [[touches allObjects] objectAtIndex: 0];
CGPoint currentPos = [myTouch locationInView: self.view];
NSLog(#"Point in myView: (%f,%f)", currentPos.x, currentPos.y);
UIView*hover = [[UIView alloc] initWithFrame:CGRectMake(currentPos.x - 50, currentPos.y - 50 , 100, 100)];
hover.backgroundColor = [UIColor grayColor];
[self.view addSubview:hover];
}
try this code
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(addDynamicView:)];
self.view.userInteractionEnabled = TRUE;
tapGesture.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:tapGesture];
adding the custom view
- (void)addDynamicView:(UITapGestureRecognizer*)aGesture{
/** add view with specified frame **/
CGRect rect = CGRectMake(10, 10, 100, 100);
UIView *lView = [[UIView alloc] initWithFrame:rect];
lView.backgroundColor = [UIColor blackColor];
[self.view addSubview:lView];
}

How to disable/enable a UIPanGestureRecognizer with two separate UIBarButtonItems?

I am trying to turn off a UIPanGestureRecognizer with the click of a "Save" button. Then, I am trying to turn back on this UIPanGestureRecognizer with the click of an "Edit" button. I can figure out how to turn them off with the following code like this:
- (IBAction)saveButton:(id)sender {
buttonCount ++;
if (buttonCount > 0) {
for (_buttonField in self.view.subviews) {
_buttonField.gestureRecognizers = nil;
_buttonField.layer.borderColor = [UIColor blackColor].CGColor;
}
}
}
Yet, I have two problems. First of all, this is turning off all of the gesture recognizers, which I do not want to do. Secondly, I cannot figure out how to turn them back on. For this I have tried the following:
- (IBAction)editButton:(id)sender {
buttonCount ++;
if (buttonCount > 0) {
if ([[UIColor colorWithCGColor:_buttonField.layer.borderColor] isEqual:[UIColor whiteColor]]) {
for (_buttonField in self.view.subviews) {
_buttonField.gestureRecognizers = YES;
}
}
}
}
However, I receive an error on the line of code that is set to YES.
Here is my complete set of code for reference:
#implementation FieldGoalChartViewController
{
}
-(void)viewDidLoad{
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {
UITouch *aTouch = [touches anyObject];
CGRect buttonRect = self.buttonField.frame;
CGPoint point = [aTouch locationInView:self.buttonField.superview];
if (!CGRectContainsPoint(buttonRect, point)) {
_buttonField.layer.borderColor = [UIColor blackColor].CGColor;
_draggedView.layer.borderColor = [UIColor blackColor].CGColor;
for (_buttonField in self.view.subviews) {
_buttonField.layer.borderColor = [UIColor blackColor].CGColor;
}
}
}
- (void)longPress:(UILongPressGestureRecognizer*)gesture {
if ( gesture.state == UIGestureRecognizerStateBegan ) {
gesture.view.layer.borderColor = [UIColor whiteColor].CGColor;
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:#"Would you like to delete the selected rep(s)?"
message:nil
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* deleteButton = [UIAlertAction
actionWithTitle:#"Delete"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
for (_buttonField in self.view.subviews) {
if ([[UIColor colorWithCGColor:_buttonField.layer.borderColor] isEqual:[UIColor whiteColor]]) {
[_buttonField removeFromSuperview];
}
}
[alert dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* cancelButton = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:deleteButton];
[alert addAction:cancelButton];
[self presentViewController:alert animated:YES completion:nil];
}
}
- (void)panWasRecognized:(UIPanGestureRecognizer *)panner {
{
panner.view.layer.borderColor = [UIColor whiteColor].CGColor;
_draggedView = panner.view;
CGPoint offset = [panner translationInView:_draggedView.superview];
CGPoint center = _draggedView.center;
_draggedView.center = CGPointMake(center.x + offset.x, center.y + offset.y);
_draggedView.layer.masksToBounds =YES;
_buttonField.layer.borderWidth = 3.0f;
// Reset translation to zero so on the next `panWasRecognized:` message, the
// translation will just be the additional movement of the touch since now.
[panner setTranslation:CGPointZero inView:_draggedView.superview];
}
}
-(void)buttonTouched:(UIButton*)sender forEvent:(id)tap {
NSSet *touches = [tap allTouches];
UITouch *touch = [touches anyObject];
UITouchPhase *phase = touch.phase;
touch.view.layer.borderColor = [UIColor whiteColor
].CGColor;
}
-(void)doubleTapped:(UIButton*)sender forEvent:(id)twoTaps {
NSSet *touches = [twoTaps allTouches];
UITouch *touch = [touches anyObject];
UITouchPhase *phase = touch.phase;
touch.view.layer.borderColor = [UIColor blackColor].CGColor;
}
- (IBAction)saveButton:(id)sender {
buttonCount ++;
if (buttonCount > 0) {
for (_buttonField in self.view.subviews) {
_buttonField.gestureRecognizers = nil;
_buttonField.layer.borderColor = [UIColor blackColor].CGColor;
}
}
}
- (IBAction)editButton:(id)sender {
buttonCount ++;
if (buttonCount > 0) {
if ([[UIColor colorWithCGColor:_buttonField.layer.borderColor] isEqual:[UIColor whiteColor]]) {
for (_buttonField in self.view.subviews) {
_buttonField.gestureRecognizers = YES;
}
}
}
}
- (IBAction)addRepButton:(UIBarButtonItem *)newRep {
self.labelCounter++;
buttonCount ++;
if (buttonCount > 0 )
{
_buttonField = [[UIButton alloc]initWithFrame:CGRectMake(300, 300, 28, 28)];
[_buttonField setTitle:[NSString stringWithFormat:#"%i", self.labelCounter]forState:UIControlStateNormal];
[_buttonField setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
_buttonField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
_buttonField.userInteractionEnabled = YES;
_buttonField.layer.cornerRadius = 14;
_buttonField.layer.borderColor = [UIColor blackColor].CGColor;
_buttonField.layer.borderWidth = 3.0f;
_buttonField.titleLabel.font = [UIFont boldSystemFontOfSize:13];
[_buttonField setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_buttonField.layer.backgroundColor = [UIColor blackColor].CGColor;
_buttonField.layer.masksToBounds = YES;
//Pan gesture declared in button
UIPanGestureRecognizer *panner = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panWasRecognized:)];
[_buttonField addGestureRecognizer:panner];
//Long Press gesture declared in button
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPress:)];
[self.buttonField addGestureRecognizer:longPress];
//Touch down inside declared in button
[self.buttonField addTarget:self action:#selector(buttonTouched:forEvent:) forControlEvents:UIControlEventTouchDown];
//Double Tap inside declared in button
[self.buttonField addTarget:self action:#selector(doubleTapped:forEvent:) forControlEvents:UIControlEventTouchDownRepeat];
[self.view addSubview:(_buttonField)];
}
}
#end
I want to know how to specifically turn off the UIPanGestureRecognizer with the click of my "Save" UIBarButton, and then turn the UIPanGestureRecognizer with the click of my "Edit" UIBarButton.
If you added Pan gesture in IB then you can create outlet and use the enabled property on the instance of the gesture to enable and disable the gesture, simple.
panGestureRecognizer.enabled = NO; (or)
panGestureRecognizer.enabled = YES;

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;
}
}
}

How can I detect a Touch on an image?

This is really basic stuff, but I can't seem to get it right (I'm new to programming). What I'm trying to do is, have my Xcode 5 App detect if an image with a certain name (say for ex. #"go.png" is pressed or touched. How can I do this? There are only two buttons, so I've been doing it with the (touchLocation.y and .x ...) method, but I need to have the Button being pressed method now. I have pasted the code below. I really appreciate your help everyone.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self];
switch (_flyGo) {
case HeadMenu:
if (touchLocation.y < self.size.height * 0.7) {
} else if (touchLocation.x < self.size.width * 0.3) {
[self switchNewFly:FlyTutorial];
} else {
[self giveRatingToApp];
}
break;
case FlyTutorial:
[self switchToFly];
break;
case FlyStatePlay:
[self flyPlayer];
break;
case FlyDisplayFalling:
break;
case FlyDisplayScore:
break;
case FlyDisplayDone:
if (touchLocation.x < self.size.width * 0.6) {
[self switchNewFly:FlyDisplayTutorial];
} else {
[self ShareMyScore];
}
break;
}
//I would like to get an image for ex. "flyer.png" to be detected as a touch instead of using (touchLocation.x or .y < self.size.width * 0.6)
Thanks!!!!
Yes, you can
Add touch recogniser to UIImageView (don't forget to set imgView.userInteractionEnabled = YES;
). Also set tag property for imgView to some value.
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapAll:)];
tapRecognizer.numberOfTapsRequired = 1;
tapRecognizer.numberOfTouchesRequired = 1;
[imgView addGestureRecognizer:tapRecognizer];
Then
- (void)handleTapAll:(UITapGestureRecognizer *)recognizer {
UIImageView *img = (UIImageView *)recognizer.view;
if(img.tag == some_vale) //your code
}
ViewController.h
#interface ViewController : UIViewController<UIGestureRecognizerDelegate>
......
ViewController.m
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
imageView.image = [UIImage imageNamed:#"yourimage.png"];
imageView.userInteractionEnabled = YES;
imageView.exclusiveTouch = YES;
imageView.multipleTouchEnabled = YES;
// Add tap gesture
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
[tap setNumberOfTapsRequired:1];
tap.delegate = self;
[imageView addGestureRecognizer:tap];
[self.view addSubview:imageView];
- (IBAction)handleTap:(UIGestureRecognizer *)recognizer { //Your code here }

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;
}

Resources