2014-06-15 32 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