5

嗨,我正在开发一个应用程序。我为一个视图制作了动画,从左到右和从右到左移动,并更改了该视图中包含的标签的值。但是当我单击左键或右键时,该视图会被删除新视图覆盖旧视图。所以我不想覆盖。只是我想添加新视图。我的代码是

    -(void)centerAnimation1:(id)sender
     {
         UIView *currentView = daysview;
            theWindow = daysview;
         // set up an animation for the transition between the views
        CATransition *animation = [CATransition animation];
            animation.delegate = self;
        [animation setDuration:0.4];
        [animation setType:kCATransitionMoveIn];

         if(rhtolft)
         {
             [animation setSubtype:kCATransitionFromLeft];
         }
         else
         {
             [animation setSubtype:kCATransitionFromRight];
         }
        [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
        [[theWindow layer] addAnimation:animation forKey:@"SwitchToView1"];
            [currentView removeFromSuperview];
        }


        - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{

             if(animate)
             {
                 CATransition *animation = [CATransition animation];
                 animation.delegate = self;
                 [animation setDuration:0.4];
                 [animation setType:kCATransitionMoveIn];
                 if(rhtolft)
                 {
                     [animation setSubtype:kCATransitionFromLeft];
                     rhtolft=NO;
                 }
                 else
                 {
                     [animation setSubtype:kCATransitionFromRight];                
                 }
                 [[theWindow layer] addAnimation:animation forKey:@"SwitchToView1"];
                 animate=NO;
                 [self.view addSubview:daysview];
             }
         }

只是我在左键和右键操作方法中调用 centerAnimation1 方法。调用此方法后,我更改了标签值。

4

6 回答 6

22

尝试这个

首先单击 L 到 R

     CGRect basketTopFrame = Yourview.frame;
         basketTopFrame.origin.x = 115;
     [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{ Yourview.frame = basketTopFrame; } completion:^(BOOL finished){ }];

然后 R 到 L

    CGRect napkinBottomFrame = Yourview.frame;
     napkinBottomFrame.origin.x = 0;
     [UIView animateWithDuration:0.3 delay:0.0 options: UIViewAnimationOptionCurveEaseOut animations:^{ Yourview.frame = napkinBottomFrame; } completion:^(BOOL finished){/*done*/}];
于 2013-05-03T11:04:38.147 回答
4

Use the below code,

UIView * testView = [[UIView alloc] initWithFrame:CGRectMake(20.0f, 100.0f, 300.0f, 200.0f)];
[testView setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:testView];

[UIView animateWithDuration:0.3f
                      delay:0.0f
                    options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
                 animations:^{
                     [testView setFrame:CGRectMake(0.0f, 100.0f, 300.0f, 200.0f)];
                 }
                 completion:nil];

[testView release];

And check the below link: http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_uiview-animations/

于 2013-05-03T10:51:00.080 回答
1

当按钮单击 UIView 从右向左和从左向右滑动时。

全局声明 NSString *string = @"one";

- (void)viewDidLoad
{
    [super viewDidLoad];
    stringslider = @"one";
}

Action Button "click"

-(IBAction)slider:(id)sender{
    if ([stringslider isEqualToString:@"one"]) {
        [UIView animateWithDuration:0.2
                     animations:^{[sliderview setFrame:CGRectMake(205, [sliderview frame].origin.y, 116, 275)];
                     }

                     completion:nil];
        stringslider = @"two";
    }
 else if ([stringslider isEqualToString:@"two"]) {
        [UIView animateWithDuration:0.2
                         animations:^{[sliderview setFrame:CGRectMake(342, [sliderview frame].origin.y, 116, 275)];
                         }
         completion:nil];
        stringslider = @"one";

}
}


easy uiview animation is done.
于 2014-06-03T07:14:44.227 回答
1

使用 Swift 4.2 和 Swift 5

从左到右为 UIView 设置动画,反之亦然。0 表示从左到右,1 表示从右到左

在此处输入图像描述


class AnimationView: UIView {
    enum Direction: Int {
        case FromLeft = 0
        case FromRight = 1
    }

    @IBInspectable var direction : Int = 0
    @IBInspectable var delay :Double = 0.0
    @IBInspectable var duration :Double = 0.0
    override func layoutSubviews() {
        initialSetup()
        UIView.animate(withDuration: duration, delay: delay, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.2, options: .curveEaseIn, animations: {
            if let superview = self.superview {
                           if self.direction == Direction.FromLeft.rawValue {
                               self.center.x += superview.bounds.width
                           } else {
                               self.center.x -= superview.bounds.width
                           }
                       }
        })
    }
    func initialSetup() {
        if let superview = self.superview {
            if direction == Direction.FromLeft.rawValue {
             self.center.x -= superview.bounds.width
            } else {
                self.center.x += superview.bounds.width
            }

        }
    }
}

于 2019-11-29T16:11:55.627 回答
0

尝试以下代码,我在我的应用程序中使用:

CGRect rectLeft = btnLeft.frame;
rectLeft.origin.x = rectLeft.origin.x + rectLeft.size.width;

CGRect rectRight = btnRight.frame;
rectRight.origin.x = rectRight.origin.x - rectRight.size.width;

[UIView animateWithDuration:0.5 animations:^{

    btnLeft.frame = rectLeft;
    btnRight.frame = rectRight;

} completion:nil];
于 2014-11-11T13:55:04.300 回答
0

下面的代码在 Swift 5.2 中测试

在我的应用程序中,我有 2 个标签单击我正在移动/从左向右滑动下划线 (UIView) 的任何标签,反之亦然,位于另一个标签的中心。

label1.setOnClickListener {
        UIView.animate(withDuration: 0.5) { [self] in
            underlineView.center.x = label1.center.x
        }
    }
label2.setOnClickListener {
        UIView.animate(withDuration: 0.5) { [self] in
            underlineView.center.x = label2.center.x
        }
    }

在此处输入图像描述

于 2022-02-22T11:22:52.753 回答