It looks like you're new here. If you want to get involved, click one of these buttons!
- (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];
}
}
Replies
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.
Duncan C
WareTo
Animated GIF created with Face Dancer, available for free in the app store.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeSet a breakpoint at the line that adds your gesture recognizer, and check the value of flakeView to make sure it's not nil
Duncan C
WareTo
Animated GIF created with Face Dancer, available for free in the app store.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeDuncan C
WareTo
Animated GIF created with Face Dancer, available for free in the app store.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome