UIView transitionWithView动画属性更改:从右侧滑入?

时间:2014-05-18 01:37:37

标签: ios iphone objective-c animation uitextview

我有一个只读的UITextView,我正在更新文本。 当新文本出现时,我希望它按照以下方式设置动画:新文本从右侧滑入,因为旧文本从屏幕左侧向左侧滑动。

是否可以使用transitionWithView执行此操作?如果没有,最好的方法是什么?我可以让它做一个CrossDissolve,但那不是我正在寻找的动画:

    [UIView transitionWithView:self.storyTextView
                  duration:0.5
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:^{
                    [self.storyTextView setText:withStory.storyText];
                }
                completion:nil];

坚持右到左动画的原因是因为我最终希望用户能够通过向UITextView左侧滑动来触发它。

3 个答案:

答案 0 :(得分:28)

CATransition将允许您执行此操作,其过渡类型为' push'和来自右边的'的子类型。它非常简单易用:

CATransition *transition = [CATransition new];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;

// Make any view changes
self.textView.text = newText;

// Add the transition
[self.textView.layer addAnimation:transition forKey:@"transition"];

请注意,虽然这是您所要求的,但如果没有一些额外的修补,就不会很好地适应手势 - 将其与您需要的手势联系起来做一些事情,比如将图层速度设置为0并手动更新动画的进度以匹配您的手势。

答案 1 :(得分:1)

如何使用UIView animateWithDuration并将一个textView向右滑动,同时从左侧滑动新的textView。这是一个例子,只需要解决这些问题。如果这是你想要做的事,请告诉我。

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.textView2.frame = CGRectMake(1000.0,self.textView1.frame.origin.y,self.textView2.frame.size.width,self.textView2.frame.size.height);

}
- (IBAction)animate:(id)sender {
    [UIView animateWithDuration:0.5 animations:^{
        self.textView1.frame = CGRectMake(-200.0,self.textView1.frame.origin.y,self.textView1.frame.size.width,self.textView1.frame.size.height);

        [UIView animateWithDuration:0.5 animations:^{
            self.textView2.center = (CGPointMake(200, 200));
        }];
    }];

}

答案 2 :(得分:1)

您也可以尝试这样的事情。

您可以为文本视图创建一个containerView,然后使用UIView动画更改坐标,使其从右向左滑动。

请参阅以下代码。

首先声明一个容器视图,然后将所需的文本视图作为其子视图。

在.h

@property (strong, nonatomic)IBOutlet UIView *containerView;
@property (strong, nonatomic)IBOutlet UITextView *childTextView; //I have set it as subview in xib

在.m中,请务必在viewDidLoad或Xib中设置以下内容

self.containerView.clipsToBounds = YES;

手势

UISwipeGestureRecognizer *recog = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(changeText)];
recog.direction = UISwipeGestureRecognizerDirectionLeft;
[self.containerView addGestureRecognizer:recog];

美化(为此你需要QuartzCore / QuartzCore.h)

self.containerView.layer.borderWidth = 1.0f;
self.containerView.layer.borderColor = [UIColor lightGrayColor].CGColor;
self.containerView.layer.cornerRadius = 5.0f;

到点

- (void)changeText
{
    [UIView animateWithDuration:0.3f animations:^(void){
        CGRect endPos = self.childTextView.frame;
        endPos.origin.x -= endPos.size.width; //Move it out of the view's frame to the left
        self.childTextView.frame = endPos;
    } completion:^(BOOL done){
        CGRect startPos  = self.childTextView.frame;
        startPos.origin.x += (2*startPos.size.width);// this will take it to the right.
        self.childTextView.frame = startPos;
        self.childTextView.text = @"Changed text";

        [UIView animateWithDuration:0.2f animations:^(void){
            CGRect displayPos = self.childTextView.frame;
            displayPos.origin.x -= displayPos.size.width; // To compensate for the double +ve on top
            self.childTextView.frame = displayPos;
        }];
    }];
}
相关问题