ios 如何从左到右移动uiview,反之亦然

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16357005/
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 23:26:37  来源:igfitidea点击:

How to move the uiview from left to right and vice versa

iphoneios

提问by venkat

Hi I am developing one application.In that i did the animation for one view to move from left to right and right to left and changing the values for labels contained in that view.But that view is removed when i click left or right button and new view is overriding the old view.So i don't want to overriding.Just i want to add new view.My code is

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

    -(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];
             }
         }

Just i call the centerAnimation1 method in left nad right button action method.After calling this one,i changed the label values.

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

回答by SAMIR RATHOD

try this

尝试这个

First clik L to R

首先点击 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){ }];

then R To L

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

回答by venkat

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/

并查看以下链接:http: //mobile.tutsplus.com/tutorials/iphone/ios-sdk_uiview-animations/

回答by mano

when Button click UIView slide right to left and left to right.

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

Global Declare NSString *string = @"one";

全局声明 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.

回答by Rashid Latif

Using Swift 4.2 and Swift 5

使用 Swift 4.2 和 Swift 5

Animate UIView from left to right or vice versa. 0 for Left to right and 1 for Right to left

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

enter image description here

在此处输入图片说明


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
            }

        }
    }
}

回答by Mohammad Ashraful Kabir

Try the following code, I'm using in my apps:

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

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];