ios UIView 动画和完成块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14900916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:18:37  来源:igfitidea点击:

UIView Animate and completion block

iosobjective-c

提问by Padin215

I'm trying to animate a tableview's offset down, then in the completion block would move it back up. But my code is not doing anything in the completion block:

我正在尝试为 tableview 的偏移设置动画,然后在完成块中将其向上移动。但是我的代码在完成块中没有做任何事情:

-(void)viewDidAppear:(BOOL)animated {
    if (TRUE) {
         NSLog(@"animating table view");

        [UIView animateWithDuration:.25
                         animations:^{
                             self.tableView.contentOffset = CGPointMake(self.tableView.contentOffset.x, self.tableView.contentOffset.y - 60);
                         }
                         completion:^(BOOL finished){
                             NSLog(@"completion block");
                         }];
    }
}

"completion block" never gets outputted... any ideas?

“完成块”永远不会被输出......有什么想法吗?

EDIT:

编辑:

Ok, so it has something to do with my UIREfreshControl:

好的,所以它与我的 UIREFreshControl 有关:

- (void)viewDidLoad
{
    [super viewDidLoad];

    if (TRUE) {
        UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
        refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to Refresh"];
        [refresh addTarget:self action:@selector(refreshTableView:) forControlEvents:UIControlEventValueChanged];

        [self setRefreshControl:refresh];
    }
}

When the refresh controll is added, it won't fire off the completion block. if i dont' add the control, it works as intended.

添加刷新控件后,它不会触发完成块。如果我不添加控件,它会按预期工作。

EDIT 2:

编辑2:

K, so if i scroll the table view, the completion block is then fired:

K,所以如果我滚动表格视图,完成块就会被触发:

2013-02-15 13:37:06.266 [1922:14003] animating table view
2013-02-15 13:37:14.782 [1922:14003] completion block

the code as written should log "completion block" right after "animating table view", but it has a 8 second delay cause thats when i scrolled the table view myself.

编写的代码应该在“动画表格视图”之后立即记录“完成块”,但它有 8 秒的延迟,原因是我自己滚动表格视图时。

how the "Pull to Refresh" looks like:

“拉动刷新”的样子:

enter image description here

在此处输入图片说明

采纳答案by pgb

I was able to reproduce this issue and found a workaround.

我能够重现此问题并找到了解决方法。

When using a UIScrollView, which UITableViewinherits from, you can't change its contentOffsetis not an animatable property. Instead, you need to use the method setContentOffset:animated:.

当使用UIScrollView,它UITableView继承了,你不能改变它contentOffset不是一个动画属性。相反,您需要使用方法setContentOffset:animated:

So, what you need to do is as follows:

所以,你需要做的是:

  1. Set your view controller as the UITableViewdelegate. I did in on viewDidAppear.
  2. Set a flag, so you know in the delegate that the scroll happened because you triggered.
  3. In the delegate method - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView, bounce back the animation (you could here add a delay by using performSelector:afterDelay:.
  1. 将您的视图控制器设置为UITableView委托。我做了viewDidAppear
  2. 设置一个标志,以便您在委托中知道滚动发生是因为您触发了。
  3. 在委托方法中- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView,反弹动画(您可以在此处使用performSelector:afterDelay:.

Here's the code:

这是代码:

@interface MyViewController ()

@property (assign, nonatomic) BOOL shouldReturn;

@end

@implementation MyViewController

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (TRUE) {
        NSLog(@"animating table view");
        self.shouldReturn = YES;
        self.tableView.delegate = self;
        [self.tableView setContentOffset:
                     CGPointMake(self.tableView.contentOffset.x,
                                 self.tableView.contentOffset.y - 60)
                                animated:YES];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (TRUE) {
        UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
        refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to Refresh"];
        [refresh addTarget:self action:@selector(refreshTableView:) forControlEvents:UIControlEventValueChanged];

        [self setRefreshControl:refresh];
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    if (self.shouldReturn) {
        self.shouldReturn = NO;
        [self.tableView setContentOffset:CGPointZero animated:YES];
    }
}

@end

回答by Guru

I used this and working fine. Its alternative solution.

我使用了这个并且工作正常。它的替代解决方案。

[UIView animateWithDuration:0.5
                              delay:0.1
                            options: UIViewAnimationOptionCurveEaseOut
                         animations:^
         {
             CGRect frame = self.adBannerView.frame;

             self.adBannerView.frame = frame;
         }
                         completion:^(BOOL finished)
         {
         }];

try adding delay and options.

尝试添加延迟和选项。