1

我有一个小代码来显示图像 1 并在 2 秒后将 image1 替换为 image2 并带有下面的动画

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 从下面的超级视图中删除 view1:

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

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

4

4 回答 4

2

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

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

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

和您的选择器:

[UIView setAnimationDidStopSelector:@selector(removeView)];
于 2012-07-18T10:36:37.027 回答
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];
}];

记得投票和/或标记为已接受的答案;)

于 2012-07-18T10:44:12.990 回答
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:)];
于 2012-07-18T10:46:14.680 回答
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];
}
于 2012-07-18T11:58:28.157 回答