iPhone - Using UIView Animations on touched location
I am stumped here as of right now, I wish to enact a UIView Animation on a finger press, by creating a UIView around where the finger is placed. Is this possible?
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
CGPoint pos = [touch locationInView: [UIApplication sharedApplication].keyWindow];
NSLog(@"Position of touch: %.3f, %.3f", pos.x, pos.y);
//CGRect touchFrame = CGRectMake(pos.x, pos.y, 100, 100);
UIView *box = [[UIView alloc] initWithFrame:CGRectMake(pos.x, pos.y, 100, 100)];
NSLog(@"%f", box.frame.origin.x);
[self.view addSubview:box];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1开发者_Go百科.0];
[UIView setAnimationTransition:110 forView:box cache:NO];
[UIView commitAnimations];
[box removeFromSuperview];
[box release];
}
Any suggestions are more than welcome.
Its due to u written following line
[box removeFromSuperview];
[box release];
This line u have to call after completion of box image animation.
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch * touch = [touches anyObject];
CGPoint pos = [touch locationInView: [UIApplication sharedApplication].keyWindow];
UIView *box = [[UIView alloc] initWithFrame:CGRectMake(pos.x, pos.y, 100, 100)];
[self.view addSubview:box];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:110 forView:box cache:NO];
[UIView commitAnimations];
[self performSelector:@selector(releaseBoxImge:) withObject:box afterDelay:1.0];
}
-(void)releaseBoxImge:(UIView *)boxImage
{
[boxImage removeFromSuperview];
[boxImage release];
}
Like RRB said, do the removeFromSuperView
after completion of the animation (I'm not sure sure his code would). It should look like this, I think:
//initializations of everything here ..
[UIView animateWithDuration:1.0
animations:^
{
//do animations here
}
completion:^(BOOL finished)
{
//do clean up here
}];
精彩评论