2014-01-12 74 views
0

如果我在這兩者之間切換數百次會發生什麼?將留下未使用的UIView s的漢德? 我在主UIView上有幾個控件,我想多次將它們作爲一組控件進行動畫(向上滑動和向下滑動),然後我決定將它們添加到UIView併爲該視圖添加動畫。但似乎每次當我將view添加到superView時,它仍然存在,如果我運行[view removeFromSuperView],它還會刪除我已添加到view的所有控件。它會影響性能還是會導致內存泄漏問題?移除動畫後的UIView,而不會丟失其他元素

(void)slideUp{ 
    repeatViewTop = datePickerButton.frame.origin.y; 
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, repeatViewTop)]; 
    [view addSubview:taskTitle]; 
    [view addSubview:taskNote]; 
    [view addSubview:datePickerButton]; 
    [view addSubview:taskSelectedDateViewer]; 
    [self.view addSubview:view]; 

    [UIView animateWithDuration:0.4f 
          delay:0.1f 
         options:UIViewAnimationOptionCurveEaseOut 
        animations:^{ 
         float tempY=view.frame.origin.y; 
         [view setCenter:CGPointMake(SCREEN_WIDTH/2,tempY)]; 
         [view setAlpha:0.4f]; 
        } 
        completion:^(BOOL finished){ 
        }]; 
    [self.view insertSubview:view atIndex:0]; 
} 

這會滑下這些控件:

- (void)slideDown{ 
    taskTitle.userInteractionEnabled=NO; 
    //150=view.frame.origin.y after sliding up 
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -114, SCREEN_WIDTH, repeatViewTop)]; 
    [view addSubview:taskTitle]; 
    [view addSubview:taskNote]; 
    [view addSubview:datePickerButton]; 
    [view addSubview:taskSelectedDateViewer]; 
    [self.view addSubview:view]; 

    [UIView animateWithDuration:0.4f 
          delay:0.1f 
         options:UIViewAnimationOptionCurveEaseOut 
        animations:^{ 
         float tempY=view.frame.origin.y *(-1); 
         [view setCenter:CGPointMake(SCREEN_WIDTH/2,tempY)]; 
         [view setAlpha:1.0f]; 
        } 
        completion:^(BOOL finished){ 


        }]; 
    [self.view insertSubview:view atIndex:0]; 
} 
+1

你應該只在一次視圖**中添加元素**,並保持對視圖的引用,然後對其進行動畫處理,而不是一直插入。 – Lefteris

+0

@Lefteris如何做到這一點?我是iOS編程新手 – Maysam

+0

'@property(strong,nonatomic)UIView * viewA'並初始化一次,然後作爲子視圖添加到'viewDidLoad'方法中。 –

回答

1

你應該在視圖中添加元素只有一次,並保持引用您的看法,然後進行動畫處理它,而不是插入它所有的時間。

您可以在viewDidLoad代表隨時創建視圖一樣,就像這樣:

- (void)viewDidLoad { 
    //Other code here 


    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, repeatViewTop)]; 
    view.tag = 1999; 
    [view addSubview:taskTitle]; 
    [view addSubview:taskNote]; 
    [view addSubview:datePickerButton]; 
    [view addSubview:taskSelectedDateViewer]; 
    [self.view addSubview:view]; 
} 

然後得到像這樣在你的動畫視圖的參考:

(void)slideUp{ 
    repeatViewTop = datePickerButton.frame.origin.y; 
    UIView *view = [self.view viewWithTag:1999]; 

    [UIView animateWithDuration:0.4f 
          delay:0.1f 
         options:UIViewAnimationOptionCurveEaseOut 
        animations:^{ 
         float tempY=view.frame.origin.y; 
         [view setCenter:CGPointMake(SCREEN_WIDTH/2,tempY)]; 
         [view setAlpha:0.4f]; 
        } 
        completion:^(BOOL finished){ 
        }]; 
} 
+0

謝謝你的工作。 – Maysam

+0

我只會添加'viewDidLoad'調用'super'作爲第一件事,它現在不會改變任何東西,但這是一個很好的做法 –

相關問題