2012-07-18 34 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]; 
}