2014-06-15 33 views
0

我有這個問題,我已經設置了並且在觸摸其中的一個之後他們必須移動,所以我使用動畫實現了此功能,並且工作正常。但在動畫完成後,我正在執行一個模態賽格,問題在於模態賽格開始時,從底部提高新的UIView,返回到他們以前的位置,在那裏我將它們設置在Storyboard中,並且它看起來很糟糕。我怎麼解決這個問題? 下面是我使用在動畫之後執行塞格動畫,但動畫物品恢復到原來的位置

@property (strong, nonatomic) IBOutlet UIButton *maleBtn; 
@property (strong, nonatomic) IBOutlet UIButton *femaleBtn; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [_maleBtn addTarget:self 
       action:@selector(chooseGender:) 
     forControlEvents:UIControlEventTouchUpInside]; 
    [_femaleBtn addTarget:self 
        action:@selector(chooseGender:) 
     forControlEvents:UIControlEventTouchUpInside]; 

} 

- (IBAction)chooseGender:(id)sender 
{ 
    CGRect maleFrame; 
    CGRect femaleFrame; 

    if (sender == _maleBtn) { 
     maleFrame = self.maleBtn.frame; 
     maleFrame.origin.x = (self.view.bounds.size.width/2) - 15; 

     femaleFrame = self.femaleBtn.frame; 
     femaleFrame.origin.x = self.view.bounds.size.height; 
    } 
    else { 
     maleFrame = self.maleBtn.frame; 
     maleFrame.origin.x = -self.view.bounds.size.height; 

     femaleFrame = self.femaleBtn.frame; 
     femaleFrame.origin.x = (self.view.bounds.size.width/2) - 15; 

    } 

    [UIView animateWithDuration:0.5 
          delay:0.5 
         options:UIViewAnimationCurveEaseOut 
        animations:^{ 
         _maleBtn.frame = maleFrame; 
         _femaleBtn.frame = femaleFrame; 
        } 
        completion:^(BOOL finished){ 
         if(sender == _maleBtn) 
          [self performSegueWithIdentifier:@"toMale" sender:self]; 
         else 
          [self performSegueWithIdentifier:@"toFemale" sender:self]; 
        }]; 
} 
+0

你有什麼東西在你的viewWillAppear?你有沒有嘗試過使用CGRectMake進行定位? –

+0

沒有這是我的所有.m沒有別的,只是我在故事板中的那些位置設置按鈕 – r4id4

+0

只需要在你的完成嘗試它,把它放在:_maleBtn.frame = CGRectMake(100,100,20,20) ;在賽前。執行搜尋並關閉並查看按鈕是否已變回原始位置。或者如果它調整大小並移動。 –

回答

0

頂部的代碼,你犯了一個布爾值或在您的.h添加屬性,如果你喜歡。但我只是使用一個局部變量。

BOOL didAnimate; 

在viewDidLoad中,你將它設置

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    //Set the bool value here to no; 
    didAnimate = NO; 

    [_maleBtn addTarget:self 
       action:@selector(chooseGender:) 
     forControlEvents:UIControlEventTouchUpInside]; 
    [_femaleBtn addTarget:self 
        action:@selector(chooseGender:) 
     forControlEvents:UIControlEventTouchUpInside]; 

} 

用在這裏,我也冒昧地重寫代碼位。

- (IBAction)chooseGender:(id)sender 
{ 
    CGRect maleFrame; 
    CGRect femaleFrame; 

    if (sender == _maleBtn) { 
     maleFrame = self.maleBtn.frame; 
     maleFrame.origin.x = (self.view.bounds.size.width/2) - 15; 

     femaleFrame = self.femaleBtn.frame; 
     femaleFrame.origin.x = self.view.bounds.size.height; 
    } 
    else { 
     maleFrame = self.maleBtn.frame; 
     maleFrame.origin.x = -self.view.bounds.size.height; 

     femaleFrame = self.femaleBtn.frame; 
     femaleFrame.origin.x = (self.view.bounds.size.width/2) - 15; 

    } 

    [UIView animateWithDuration:0.5 
          delay:0.5 
         options:UIViewAnimationCurveEaseOut 
        animations:^{ 
         _maleBtn.frame = maleFrame; 
         _femaleBtn.frame = femaleFrame; 
        } 
        completion:^(BOOL finished){ 
        didAnimate = YES; 
        }]; 

    if(sender == _maleBtn && didAnimate){ 
      [self performSegueWithIdentifier:@"toMale" sender:self]; 
    } 
    else if((sender == _femaleBtn && didAnimate)){ 
      [self performSegueWithIdentifier:@"toFemale" sender:self]; 
    } 
    else{ 
    NSLog(@"failure to transition"); 
    } 
} 

我還沒有測試過這個,有可能是拼寫錯誤等。所以複製/粘貼可能不起作用。

+0

它不工作只是因爲if語句爲segue被稱爲'BEFORE'完成處理程序,所以'didAnimate'總是錯誤的。我嘗試了類似的東西,並且遇到了代碼中的這個問題。把它放在完成處理程序中是沒用的,所以我不能提出解決方案。 – r4id4

+0

你解決了嗎?正如我想到解決您的問題。 –

+0

不,我還沒有!你有什麼發現? – r4id4