Hifellows,請考慮以下情形:取消/停止的執行的方法(選擇器) - Objective-C中的iOS
我有3種方法,其中在每個創建並添加某些對象像imageviews,activityindicators等。
所以,想象一下:
- (void)anim1 {
// do some verifications like internet reachability. So create imageview1, imageview2, imageview3, activityindicator then add to subView.
[UIView animateWithDuration:1 delay:0.3 options:0 animations:^{
// Animate the alpha value of your imageView from 1.0 to 0.0 here
//...
} completion:^(BOOL finished) {
if(![(AppDelegate*)[[UIApplication sharedApplication] delegate] hasConnectivity])
{
NSLog(@"No Internet...");
return;
}
else {
// If i got internet connection, i create another imageview (checked image) and add to subview. So play with alpha animations then call the second method.
[UIView animateWithDuration:0.8 delay:0.8 options:0 animations:^{
someobject.alpha = 0.0f;
[someobject setAlpha:0.5f];
[someobject setAlpha:0.5f];
} completion:^(BOOL finished) {
[self performSelector:@selector(anim2) withObject:nil afterDelay:0.1];
NSLog(@"Got Internet...");
}];
return;
}
}];
}
所以...... anim2:
- (void)anim2 {
// do some URL connections (POST FORM) with blocks using AFNetworking framework. So, depending on results of URL connection, i work with imageview2, add an activityindicator (while URL processing) then add to subView. To resume, let consider this:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.somepage.com"]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Response OK..");
// Here i create another imageview (checked image) and add to subview. So play with alpha animations then call the third method.
[UIView animateWithDuration:0.8 delay:0.8 options:0 animations:^{
someobject.alpha = 0.0f;
[someobject setAlpha:0.5f];
[someobject setAlpha:0.5f];
} completion:^(BOOL finished) {
[self performSelector:@selector(anim3) withObject:nil afterDelay:0.1];
}];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Response error. Aborting.");
}
}];
[operation start];
}
最後,anim3方法:
- (void)anim3 {
// Work with imageview3, another activityindicator, then after animation delay, i create a new imageview (checked image). Then enable a uibutton.
...
}
所以,當我開始anim1,它做驗證,然後按照'sequence',調用anim2 ...等等... Anim1 = OK - > Anim2 = OK - > Anim3 - > Button enabled - > Clicked - >將所有圖像視圖設置爲alpha = 0.70f。
所有的工作,以及我已經創建了另一種方法來'清理'這些意見,然後再次開始。
基本上,它取消所有選擇器,然後從fromSuperView中刪除所有已創建的圖像。然後,再次呼叫anim1。
請考慮:
-(void)cleanAnimViews
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(anim1) object:nil];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(anim2) object:nil];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(anim3) object:nil];
NSArray* subviews = [[NSArray alloc] initWithArray: scrollview.subviews];
for (UIView* view in subviews) {
if ([view isKindOfClass:[UIActivityIndicatorView class]]) {
[view removeFromSuperview];
}
}
for (UIView* view in subviews) {
if ([view isKindOfClass:[UIImageView class]] && view.tag == 200) {
[view removeFromSuperview];
}
if ([view isKindOfClass:[UIImageView class]] && view.tag == 201) {
[view removeFromSuperview];
}
if ([view isKindOfClass:[UIImageView class]] && view.tag == 202) {
[view removeFromSuperview];
}
if ([view isKindOfClass:[UIImageView class]] && view.tag == 203) {
[view removeFromSuperview];
}
if ([view isKindOfClass:[UIActivityIndicatorView class]] && view.tag == 204) {
[view removeFromSuperview];
}
if ([view isKindOfClass:[UIImageView class]] && view.tag == 250) {
[view removeFromSuperview];
}
}
[self performSelector:@selector(anim1) withObject:nil afterDelay:0.8];
}
我打電話 'cleanAnimViews' 上viewWillAppear中,並使用RefreshControl對象。 我的問題: 當所有任務(想anim1,anim2和anim3)已經完成,那麼我所說的「cleanAnimViews」使用上拉刷新,它精美刪除所有imageviews然後從第一種方法(anim1)再次啓動。
可是,如果我叫animX期間「cleanAnimViews」使用上拉,刷新等待處理(anim1之間anim2,或anim2到anim3),我得到了混亂,因爲「cleanAnimViews」刪除所有imageviews,但「anim1 - > anim2 - > anim3'繼續在同一時間處理...所以,我得到了兩個(或三個)activityindicator(例如:來自anim1和anim3),與對象控件混淆,因爲它在anim2上執行較舊的「proccess」 (EG)和新的Sametime中以anim1 ...
讓我更好地解釋:
viewDidAp梨 - >
開始anim1: ...這創造了3個圖像...創建ActivityIndicator ...驗證做......創建一個 「檢查」 imageview的......所以進行選擇anim2。
開始anim2 ... ......這適用對象的控制,創建ActivityIndicator,驗證做......創建一個 「檢查」 的ImageView ...
---->NOW我稱之爲「cleanAnimViews」從拉動刷新。但anim2已經進行選擇anim3 ...
因此,它會刪除所有的ImageView和方法的結束(從cleanAnimViews)重新開始anim1,並作爲anim3方法已經開始,它創造imageviews,activityindicator並再次檢查圖像..和anim1是在同一時間再次做所有的過程..
有一種方法來取消/停止這些animX方法,當我打電話'cleanAnimViews'? 我問這是因爲當視圖消失,並再次出現所有作品美麗。它停止所有animX,當再次出現再次撥打anim1 ...
我試圖在「cleanAnimViews」創建BOOL標誌,然後在animX方法用它玩..但是不工作..
對不起長帖子,錯別字,如果我沒有那麼清楚......但我沒有更多的想法。
在此先感謝。
在每種方法(anim1,anim2,anim3)上我都有一個實例化的uiimageview對象,可以調用someObject1,someObject2和someObject3。 所以對anim1: [mySuperView addSubview:someObject1];動畫2: [mySuperView addSubview:someObject2]動畫3: [mySuperView addSubview:someObject3] 然後,在「cleanAnimViews」方法中,我使用了: [someObject1.layer removeAllAnimations]; [someObject2.layer removeAllAnimations]; [someObject3.layer removeAllAnimations]; 但它不工作...(我已經有QC框架) – thiagomorales