替换为view2后删除视图1

时间:2012-07-18 10:30:15

标签: iphone ios selector

我有一个小代码来显示图像1,2秒后用image2替换image1和动画

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从下面的superview中删除view1:

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

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

4 个答案:

答案 0 :(得分:2)

选择器无法传递参数。将您的方法修改为

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

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

和您的选择器:

[UIView setAnimationDidStopSelector:@selector(removeView)];

答案 1 :(得分: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];
}];

请记住将其投票或标记为已接受的答案;)

答案 2 :(得分: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:)];

答案 3 :(得分:0)

问题很简单,这个选择器存在于您的班级中:-removeView:view1:。因此在动画结束后没有任何东西可以回复。这就是为什么你的-(void)removeView:(UIImageView *)view1;方法永远不会被回调的原因。

请注意,您的真实选择器为-removeView:且不等于-removeView:view1:

如果你想通过didStopSelector传递参数,我会有一个坏消息:你不能像你在代码中那样做,所以这部分是错误的:

// 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];
}