Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-11-2011, 09:50 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 12
waq963 is on a distinguished road
Default UITouch Multiple Images

Hi,

I am trying to get a touch event every time an image is touched to update a label and be removed from view. However if I have a single image the touch event below works but because I am animated many images it cannot detect it. I would appreciate any help.

Thanks

Code:
- (void)viewDidLoad {
	[super viewDidLoad];
	score = 0;
	flakeImage = [UIImage imageNamed:@"flake.png"];
	[NSTimer scheduledTimerWithTimeInterval:(0.5) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}

- (void)onTimer
{
	flakeView = [[UIImageView alloc] initWithImage:flakeImage];
    flakeView.opaque = YES;
    flakeView.userInteractionEnabled = YES;
    flakeView.multipleTouchEnabled = YES;
    
	int startX = round(random() % 320);
	int endX = round(random() % 320);
	double scale = 1 / round(random() % 100) + 1.0;
	double speed = 1 / round(random() % 100) + 1.0;
	
	self.flakeView.frame = CGRectMake(startX, -100.0, 25.0 * scale, 25.0 * scale);
    
	[self.view addSubview:self.flakeView];
    [self.view bringSubviewToFront:self.flakeView];
	
    [UIView animateWithDuration:5 * speed delay:0.0 options: UIViewAnimationOptionAllowUserInteraction animations:^{
        self.flakeView.frame = CGRectMake(endX, 500.0, 25.0 * scale, 25.0 * scale);
    } completion:nil];

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];
    if ([self.flakeView.layer.presentationLayer hitTest:touchLocation])
    {
        NSLog(@"Image was touched");
        [self.flakeView removeFromSuperview];
        score = score + 1;
        lbl1.text = [NSString stringWithFormat:@"%i", score];
    }
}
waq963 is offline   Reply With Quote
Old 12-11-2011, 05:52 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,005
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by waq963 View Post
Hi,

I am trying to get a touch event every time an image is touched to update a label and be removed from view. However if I have a single image the touch event below works but because I am animated many images it cannot detect it. I would appreciate any help.

Thanks

Code:
- (void)viewDidLoad {
	[super viewDidLoad];
	score = 0;
	flakeImage = [UIImage imageNamed:@"flake.png"];
	[NSTimer scheduledTimerWithTimeInterval:(0.5) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}

- (void)onTimer
{
	flakeView = [[UIImageView alloc] initWithImage:flakeImage];
    flakeView.opaque = YES;
    flakeView.userInteractionEnabled = YES;
    flakeView.multipleTouchEnabled = YES;
    
	int startX = round(random() % 320);
	int endX = round(random() % 320);
	double scale = 1 / round(random() % 100) + 1.0;
	double speed = 1 / round(random() % 100) + 1.0;
	
	self.flakeView.frame = CGRectMake(startX, -100.0, 25.0 * scale, 25.0 * scale);
    
	[self.view addSubview:self.flakeView];
    [self.view bringSubviewToFront:self.flakeView];
	
    [UIView animateWithDuration:5 * speed delay:0.0 options: UIViewAnimationOptionAllowUserInteraction animations:^{
        self.flakeView.frame = CGRectMake(endX, 500.0, 25.0 * scale, 25.0 * scale);
    } completion:nil];

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];
    if ([self.flakeView.layer.presentationLayer hitTest:touchLocation])
    {
        NSLog(@"Image was touched");
        [self.flakeView removeFromSuperview];
        score = score + 1;
        lbl1.text = [NSString stringWithFormat:@"%i", score];
    }
}

I would suggest using UIGestureRecognizer objects. If all you want to respond to is taps, use UITapGestureRecognizer.

You can create tap gesture recognizers in your viewDidLoad with your view controller as the target. Add one to each view that you want to respond to taps.

You can either use different action methods for each view, or use the same action method for all of them, and check the view property of the gesture recognizer object that's passed to you to see which view was tapped.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is online now   Reply With Quote
Old 12-12-2011, 05:42 AM   #3 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 12
waq963 is on a distinguished road
Default

Thanks for the reply Duncan. Having never used gesture recognizers before i've had a go and was hoping you could point me in the right direction. I can get it to recognize taps in view but not on the Imageviews. The code below detects nothing.

Code:
- (void)viewDidLoad {
	[super viewDidLoad];
	score = 0;
	flakeImage = [UIImage imageNamed:@"flake.png"];

    UIGestureRecognizer *recognizer;
    recognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedImage:)] autorelease];
    tapping = (UITapGestureRecognizer *)recognizer;
    [tapping setNumberOfTapsRequired:1];
    [tapping setNumberOfTouchesRequired:1];
    
    [self.flakeView addGestureRecognizer:tapping];
	[NSTimer scheduledTimerWithTimeInterval:(0.5) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}

- (void)tappedImage: (UIGestureRecognizer *)recognizer
{
    [recognizer locationInView:recognizer.view];  
    NSLog(@"Action: One finger, two taps");
}
waq963 is offline   Reply With Quote
Old 12-12-2011, 09:16 AM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,005
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Vanessa5435 View Post
I would suggest using UIGestureRecognizer objects. If all you want to respond to is taps, use UITapGestureRecognizer.

You can create tap gesture recognizers in your viewDidLoad with your view controller as the target. Add one to each view that you want to respond to taps.

You can either use different action methods for each view, or use the same action method for all of them, and check the view property of the gesture recognizer object that's passed to you to see which view was tapped.
__________________



Is there an echo in here?
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is online now   Reply With Quote
Old 12-12-2011, 09:52 AM   #5 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 12
waq963 is on a distinguished road
Default

Apparently so. But it hasn't helped me solve my problem unfortunately. I shall keep going though.
waq963 is offline   Reply With Quote
Old 12-12-2011, 11:02 AM   #6 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,005
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by waq963 View Post
Thanks for the reply Duncan. Having never used gesture recognizers before i've had a go and was hoping you could point me in the right direction. I can get it to recognize taps in view but not on the Imageviews. The code below detects nothing.

Code:
- (void)viewDidLoad {
	[super viewDidLoad];
	score = 0;
	flakeImage = [UIImage imageNamed:@"flake.png"];

    UIGestureRecognizer *recognizer;
    recognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedImage:)] autorelease];
    tapping = (UITapGestureRecognizer *)recognizer;
    [tapping setNumberOfTapsRequired:1];
    [tapping setNumberOfTouchesRequired:1];
    
    [self.flakeView addGestureRecognizer:tapping];
	[NSTimer scheduledTimerWithTimeInterval:(0.5) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}

- (void)tappedImage: (UIGestureRecognizer *)recognizer
{
    [recognizer locationInView:recognizer.view];  
    NSLog(@"Action: One finger, two taps");
}
The code you posted should work. Is self.flakeView non-nil in your viewDidLoad method. Broken outlet/action links are a common cause of code that should work, but doesn't.

Set a breakpoint at the line that adds your gesture recognizer, and check the value of flakeView to make sure it's not nil
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is online now   Reply With Quote
Old 12-13-2011, 08:23 AM   #7 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 12
waq963 is on a distinguished road
Default

I have attached the project if this will make it easier to see the problem.

Last edited by waq963; 12-14-2011 at 06:43 AM.
waq963 is offline   Reply With Quote
Old 12-13-2011, 10:38 AM   #8 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,005
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by waq963 View Post
I have attached the project if this will make it easier to see the problem.
I don't have time to download and debug your project for you. That's your job.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is online now   Reply With Quote
Old 12-13-2011, 10:54 AM   #9 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 12
waq963 is on a distinguished road
Default

No worries mate, just thought it might be something simple that I wasn't aware of. Thanks for all your help anyway much appreciated.
waq963 is offline   Reply With Quote
Reply

Bookmarks

Tags
animation, touch, uiimageview, uitouch

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 400
12 members and 388 guests
7twenty7, AppsBlogger, David-T, Duncan C, HemiMG, heshiming, LunarMoon, Murphy, sacha1996, Sami Gh, teebee74, Tomsky
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,127
Posts: 402,915
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:46 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0