Iphone 将视图1替换为视图2后删除视图1
我有一个显示图像1的小代码,2秒钟后用下面的动画将图像1替换为图像2Iphone 将视图1替换为视图2后删除视图1,iphone,ios,selector,Iphone,Ios,Selector,我有一个显示图像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 = [[UI
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的功能不起作用,请帮助我!非常感谢…选择器无法传递参数。将您的方法修改为
-(void)removeView{
[view1 removeFromSuperview];
}
其中“view1”是视图的实例
以及您的选择器:
[UIView setAnimationDidStopSelector:@selector(removeView)];
我建议您使用基于块的动画(从iOS 4开始提供)。 它们更易于使用,不需要通过方法发送参数等等。 例如: 记住投赞成票,或将其标记为接受答案;) 试试这段代码
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:)];
问题很简单,该选择器在类中不存在:
-removeView:view1:
。因此,在动画完成后,没有什么可调用的。这就是为什么您的-(void)removeView:(UIImageView*)视图1代码>方法将永远不会被回调
请注意,您真正的选择器是-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];
}
感谢您的帮助,此解决方案对我来说是最好的:)
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
[((UIView *)context) removeFromSuperView];
}