【问题标题】:completion bool successful not working完成布尔成功不工作
【发布时间】:2014-02-19 21:47:55
【问题描述】:

我正在开发一个在第二个视图控制器中带有动画的应用程序。动画完成后,我试图自动切换到下一个视图控制器。我有

完成:^(BOOL 成功){...

但它不起作用。然后我尝试了一个简单的 NSLog@"... 但这也不起作用。我猜问题是 BOOL 没有完成或注册为成功。我的动画发生并完成了......但完成 BOOL 是没有执行。请帮忙。

来自 secondViewController 的 .m 文件...

#import "secondViewController.h"

@interface secondViewController ()

@property (nonatomic,weak) IBOutlet UIImageView *animatedImage;

@end

@implementation secondViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void)viewDidAppear:(BOOL)animated
{
[self moveUp:nil finished:nil context:nil];
}
- (void)moveUp:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {

[UIView animateWithDuration:2.0
                      delay:1.0
 options:      (UIViewAnimationCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction)
                 animations:^{
                     [UIView setAnimationDelegate:self];
                     [UIView setAnimationDidStopSelector:@selector(moveDown:finished:context:)];
                     self.animatedImage.center = CGPointMake(160, 304);
                 }
                 completion:^(BOOL finished){
                     NSLog(@"Move up done");

                 }];


}

- (void)moveDown:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {


[UIView animateWithDuration:2.0
                      delay:1.5
           options:  (UIViewAnimationCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction)
                 animations:^{
                     [UIView setAnimationDelegate:self];
                     [UIView setAnimationDidStopSelector:@selector(moveUp:finished:context:)];
                     self.animatedImage.center = CGPointMake(160, 775);

                 }
                 completion:^(BOOL finished){

                    [self performSegueWithIdentifier:@"backHome" sender:self];
                 }];
}

@end

【问题讨论】:

  • 你能给我们看更多的代码吗?例如,您创建动画的部分

标签: iphone objective-c animation boolean xcode5


【解决方案1】:

下面的代码会很好读。

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated]; //Invoke super method
    [self doAnimation];
}

- (void)doAnimation{
    [UIView animateWithDuration:2.0
                      delay:1.0
                    options:(UIViewAnimationCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction)
                 animations:^{
                     self.animatedImage.center = CGPointMake(160, 304);
                 }
                 completion:^(BOOL finished){
                    [UIView animateWithDuration:2.0
                                          delay:1.5
                                        options:(UIViewAnimationCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction)
                                     animations:^{
                                           self.animatedImage.center = CGPointMake(160, 775);
                                     }
                                     completion:^(BOOL finished){
                                           [self performSegueWithIdentifier:@"backHome" sender:self];
                                     }
                   ];}

    }];
}

【讨论】:

  • 非常感谢!我试过了,好消息是完成块正在工作 - 但是 UIImageView 不再动画。它只是静止几秒钟,然后完成。有什么想法吗?
  • 深入挖掘我发现它只是第一个动画的运动没有发生。换句话说,延迟发生,CGPointMake 被跳过,下一个持续时间发生,第二个 CGPointMake 发生。知道为什么第一个 CGPointMake 被跳过了吗?
  • 想通了...在文件检查器中取消选中“使用自动布局”。现在工作完美。
  • 恭喜! @user3294722 我刚上线。 :)
【解决方案2】:

您正在结合旧式动画和基于块的动画。不要那样做。

删除

[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:...];

可能会解决问题。而是将完成逻辑放在完成块中。

【讨论】:

  • 谢谢。我删除了这两个和完成:^(布尔完成){ NSLog(@“向上移动完成”);工作,但图像不再动画。另外,抱歉,您将完成逻辑放在完成块中是什么意思?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-17
  • 1970-01-01
  • 2021-04-15
  • 1970-01-01
  • 2011-10-01
  • 2018-12-17
  • 1970-01-01
相关资源
最近更新 更多