Iphone 如何将uiview从左向右移动,反之亦然

Iphone 如何将uiview从左向右移动,反之亦然,iphone,ios,Iphone,Ios,您好,我正在开发一个应用程序。我制作了一个视图从左到右和从右到左移动的动画,并更改了该视图中包含的标签值。但是,当我单击“左”或“右”按钮时,该视图将被删除,新视图将覆盖旧视图。因此,我不想覆盖。我只想添加新视图。我的代码是 -(void)centerAnimation1:(id)sender { UIView *currentView = daysview; theWindow = daysview; // set

您好,我正在开发一个应用程序。我制作了一个视图从左到右和从右到左移动的动画,并更改了该视图中包含的标签值。但是,当我单击“左”或“右”按钮时,该视图将被删除,新视图将覆盖旧视图。因此,我不想覆盖。我只想添加新视图。我的代码是

    -(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];
             }
         }
我只需在left和right button action方法中调用centerAnimation1方法。调用此方法后,我更改了标签值。

使用以下代码

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];
然后检查下面的链接:

试试这个

第一个clik从左到右

     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*/}];

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

全局声明NSString*string=@“一”


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

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];
使用Swift 4.2和Swift 5

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


当我点击左键时,移除视图从右到左移动,视图从左到右,并添加为新视图。谢谢。只是当我点击左键时,我调用了第一个视图,更改了值,并调用了下一个视图。这是我想要的。对于右键,调用了反向。
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];

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
            }

        }
    }
}