2012-07-18 41 views
1

我有一个小代码来显示图像1和2秒后替换图像1由图像2与动画下面删除视图1代替后视图2

UIImageView *view1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 410, 1020, 400)]; 
UIImage *image = [UIImage imageNamed:@"img.jpeg"]; 
view1.image = image; 
[self.view addSubview:view1]; 
UIImageView *view2 = [[UIImageView alloc] init ]; 
view2.frame = CGRectMake(0, 410, 0, 400); 
view2.image = [UIImage imageNamed:@"bien.jpeg"]; 
[self.view addSubview:view2]; 
[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.8]; 
[UIView setAnimationDelay:2]; 
[UIView setAnimationDelegate:self]; 
[UIView setAnimationDidStopSelector:@selector(removeView: view1:)]; 
view2.frame = CGRectMake(0, 410, 800, 400); 
[UIView commitAnimations]; 

和功能removeView从下方上海华除去厂景:

-(void)removeView: (UIImageView *)view1{ 
[view1 removeFromSuperview]; 
} 

所以我不知道为什么我的功能从超级视图删除view1不起作用,请帮助我!非常感谢...

回答

1

我会建议你使用基于块的动画(因为iOS 4的可用)。 他们更容易使用,不需要通过方法和所有东西发送参数。 例子:

UIImageView *view1 = [[UIImageView alloc] init]; 
//initialize your UIImageView view1 
[self.view addSubview:view1]; 
UIImageView *view2 = [[UIImageView alloc] init]; 
//initialize your UIImageView view2 
[UIView animateWithDuration:2 animations:^{ 
    //here happens the animation 
    [self addSubview:view2]; 
} completion:^(BOOL finished) { 
    //here happens stuff when animation is complete 
    [view1 removeFromSuperView]; 
}]; 

记住投票起来,或标记为接受的答案;)

+1

真棒的想法...: - ) – Nina 2012-07-18 10:47:28

+0

@Nina,谢谢=) – 2012-07-18 10:50:35

+0

感谢您的帮助,这个解决方案对我来说是最好的:) – 2012-07-19 02:42:33

2

选择器不能传递参数。修改您的方法到

-(void)removeView{ 
    [view1 removeFromSuperview]; 
} 

其中“view1”是您的视图的实例。

和你选择到:

[UIView setAnimationDidStopSelector:@selector(removeView)]; 
0

试试这个代码!

UIImageView *view1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 410, 1020, 400)]; 
UIImage *image = [UIImage imageNamed:@"img.jpeg"]; 
view1.tag = 1; 
view1.image = image; 
[self.view addSubview:view1]; 

UIImageView *view2 = [[UIImageView alloc] init ]; 
view2.frame = CGRectMake(0, 410, 0, 400); 
view2.image = [UIImage imageNamed:@"bien.jpeg"]; 
[self.view addSubview:view2]; 

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.8]; 
[UIView setAnimationDelay:2]; 
[UIView setAnimationDelegate:self]; 
[UIView setAnimationDidStopSelector:@selector(removeView: view1:)]; 
view2.frame = CGRectMake(0, 410, 800, 400); 
[UIView commitAnimations]; 

//删除查看方法

-(void)removeView : (UIImageView *) imgVew { 

    if (imgVew.tag == 1) 

     [imgVew removeFromSuperView]; 
} 

访问它 -

[UIView setAnimationDidStopSelector:@selector(removeView:)]; 
0

的问题很简单,这个选择是存在于你的等级:-removeView:view1:。因此动画完成后没有任何回应。这就是为什么你的-(void)removeView:(UIImageView *)view1;方法将永远不会被回调。

请,意识到自己真正的选择是-removeView:,如果你想通过didStopSelector传递参数,它不与-removeView:view1:

平等的,我想有一个坏消息:你不能做到像你一样在你的代码,所以这部分是错误的,在所有:

// WRONG AT ALL:  
[UIView setAnimationDidStopSelector:@selector(removeView:view1:)]; 

// PROPER WAY: 
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 

因为didStopSelector必须是以下类型的选择与下列参数。

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context; 

可以为你的回调方法这样通参数:

[UIView beginAnimations:nil context:view1]; // see the context's value 

,并在您didStopSelector你可以用它在某种程度上喜欢:

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context { 
    [((UIView *)context) removeFromSuperView]; 
}