【问题标题】:Perform a Segue after animation but animated items get back to their previous position动画后执行 Segue,但动画项目回到之前的位置
【发布时间】:2014-06-15 10:02:25
【问题描述】:

我有这个问题,我设置了Storyboard 2 UIButton 并且在触摸其中一个之后它们必须移动,所以我使用动画实现了这个,并且工作正常。但是在动画完成后,我正在执行模态转场,问题是当模态转场开始时,从底部提升新的UIView,2 UIButton 返回到我设置它们的先前位置在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];
                     }];
}

【问题讨论】:

  • 您有什么想法会出现吗?您是否尝试过使用 CGRectMake 进行定位?
  • 不,这就是我的全部 .m 没有别的了,只是我将按钮设置在情节提要的那些位置
  • 只是为了在你的完成中尝试一下,把这个放在:_maleBtn.frame = CGRectMake(100, 100, 20, 20);在赛格之前。执行 segue 和并关闭并查看按钮是否已更改回原始位置。或者如果它已经调整大小和移动。
  • 还在恢复原位!
  • 那我怀疑是和这个帖子类似的问题stackoverflow.com/questions/21634244/…

标签: ios iphone objective-c animation uibutton


【解决方案1】:

如果您愿意,您可以在顶部创建一个布尔值或在 .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");
     }
}

我没有对此进行测试,可能存在拼写错误等。所以复制/粘贴可能不起作用。

【讨论】:

  • 它不起作用仅仅是因为segue的if语句被称为BEFORE完成处理程序,所以didAnimate总是错误的。我尝试了类似的方法,但也遇到了您的代码中存在的这个问题。把它放在完成处理程序中是没用的,所以我无法提出解决方案。
  • 你解决了吗?因为我已经想出了解决您问题的方法。
猜你喜欢
  • 1970-01-01
  • 2012-07-30
  • 2015-12-16
  • 1970-01-01
  • 2016-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多