2

我有一个 iPad 应用程序,它显示一个UILabel带有文本的“注释”(子类)。为了导航到下一个笔记,我希望它从屏幕上向左滑动,同时让下一个从右侧滑入。我可以让任一动画工作,但不能同时工作。

这是我的控制器中的代码:

- (void)slideOutLeft {
    // create the new note
    flSlidingNote *newNote = [[flSlidingNote alloc] init];
    newNote.text = @"blah blah";
    CGRect newFrame = CGRectMake(1000, 70, 637, 297); // off the right
    newNote.frame = newFrame;
    [self.view addSubview:newNote];

    // slide off the current one
    CGRect currentFrameEnd = noteLabel.frame; // noteLabel is the existing note
    currentFrameEnd.origin.x = 0 - noteLabel.frame.size.width; // off the left

    [UIView animateWithDuration:1.0 animations:^{
        noteLabel.frame = currentFrameEnd;
    } completion:nil];
}

noteLabel根本没有动画。如果我注释掉它的addSubview:newNote部分。我在这方面还是比较新的,所以它可能只是一些简单的事情。

无论是否newNote动画(代码片段中没有动画),都会出现问题。

4

3 回答 3

1

You want to put the animation for both your views and the addSubview call for your new view in one animation block, like so:

  // layout the new label like the old, but 300px offscreen right 
  UILabel * newNote = [[UILabel alloc] initWithFrame:CGRectOffset(self.noteLabelView.frame, 300, 0)];
  newNote.text = @"NEW NOTE";

 [UIView animateWithDuration:2.0f
                       delay:0
                     options:UIViewAnimationCurveEaseInOut
                  animations:^{
                     // animate the old label left 300px to offscreen left
                     self.noteLabelView.center = CGPointMake(self.noteLabelView.center.x-300, self.noteLabelView.center.y);
                     // add the new label to the view hierarchy
                     [self.view addSubview:newNote];
                     // animate the new label left 300px into the old one's spot
                    newNote.center = CGPointMake(newNote.center.x-300, newNote.center.y);
                  }
                  completion:^(BOOL finished) {
                    // remove the old label from the view hierarchy
                    [self.noteLabelView removeFromSuperview];
                    // set the property to point to the new label
                    self.noteLabelView = newNote;
               }];

In this snippet above, I'm assuming the old label is addressable via the property self.noteLabelView.

(Also have a look at https://github.com/algal/SlidingNotes , though I can't promise that will stay there long so maybe SO isn't the right format for such a link? )

于 2012-12-11T00:45:59.050 回答
1

Adding to what Gabriele Petronella commented, here is a good resource on KeyFrames and AnimationGroup, with a sample GitHub project linked:

http://blog.corywiles.com/using-caanimationgroup-for-view-animations

This really helped me understand animations better.

于 2013-02-07T13:50:37.447 回答
-1

不确定,但我见过其他代码循环遍历子视图并执行动画,您可能想尝试以下操作:

for(UIView *view in self.subviews)
{
    // perform animations 
}
于 2012-12-10T22:50:44.813 回答