Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

UITouch Multiple Images

waq963waq963 Posts: 12Registered Users
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


- (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];
}
}
Post edited by waq963 on

Replies

  • Duncan CDuncan C Posts: 8,027Tutorial Authors, Registered Users
    waq963;393693 said:
    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


    - (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

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • waq963waq963 Posts: 12Registered Users
    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.


    - (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\");
    }
  • waq963waq963 Posts: 12Registered Users
    Apparently so. But it hasn't helped me solve my problem unfortunately. I shall keep going though.
  • Duncan CDuncan C Posts: 8,027Tutorial Authors, Registered Users
    waq963;393858 said:
    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.


    - (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

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • waq963waq963 Posts: 12Registered Users
    I have attached the project if this will make it easier to see the problem.
  • Duncan CDuncan C Posts: 8,027Tutorial Authors, Registered Users
    waq963;394154 said:
    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

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • waq963waq963 Posts: 12Registered Users
    No worries mate, just thought it might be something simple that I wasn't aware of. Thanks for all your help anyway much appreciated.
Sign In or Register to comment.