【问题标题】:UIView having Animation from right to leftUIView 具有从右到左的动画
【发布时间】:2015-05-08 09:31:28
【问题描述】:

如何使 uiview 动画化,使其从右到左缓慢显示,并在其上有两个 UIButtons,按钮上有图像,其中一个按钮上的图像将在按钮单击时改变

【问题讨论】:

  • 您真的希望得到 Genie 的答复吗?首先展示你的努力和代码。
  • 开始阅读文档。一些指点:UIButtonUIView animation

标签: ios objective-c iphone animation


【解决方案1】:
[UIView animateWithDuration:0.7f
                 animations:^ {
                     CGRect frame = YourView.frame;
                     frame.origin.x = 0;
                     YourView.frame = frame;
                     YourView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);
                 }
                 completion:^(BOOL finished) {

                     [UIView beginAnimations:nil context:nil];
                     [UIView setAnimationDuration:5.3];
                     [UIView commitAnimations];
                 }];

【讨论】:

    【解决方案2】:

    您可以尝试此代码并根据您的要求进行更改。 在这里,我创建了一个从右到左移动的基本视图,反之亦然。 在那个基本视图中,您可以放置​​您想要动画的任何内容。 根据您的要求设置bounceDistance,bounceDuration,animateWithDuration,delay 变量。

    -(IBAction)rightClicked:(id)sender {
        CGRect initalFrame = self.baseView.frame;
        initalFrame.origin.x = initalFrame.size.width;
       self.baseView.frame = initalFrame;
        [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(moveFromLeftOrRight:) userInfo:@0 repeats:NO];
      }
    
    -(IBAction)leftClicked:(id)sender {
       CGRect initalFrame = self.baseView.frame;
       initalFrame.origin.x = -initalFrame.size.width;
       self.baseView.frame = initalFrame;
       [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(moveFromLeftOrRight:) userInfo:@1 repeats:NO];
      }
    
    -(void)moveFromLeftOrRight:(NSTimer *) timer {
        BOOL isLeft = [timer.userInfo boolValue];
        CGFloat bounceDistance = 10;
        CGFloat bounceDuration = 0.2;
        [UIView animateWithDuration:.2 delay:0.0 options:UIViewAnimationOptionAllowAnimatedContent
        animations:^{
         CGFloat direction = (isLeft ? 1 : -1);
         self.baseView.center = CGPointMake(self.baseView.frame.size.width/2 + direction*bounceDistance, self.baseView.center.y);}
        completion:^(BOOL finished){
         [UIView animateWithDuration:bounceDuration animations:^{ 
        self.baseView.center = CGPointMake(self.baseView.frame.size.width/2, self.baseView.center.y);
        }];
     }];
    }
    

    【讨论】:

    • 很好的例子!
    【解决方案3】:

    假设您的目标是 iPhone。

    • 在情节提要 (IB) 中将视图的 x 坐标设置为 320(在 iPhone 中最大可见)。

      x-320 y-0 width-320 height-568

    • 在 viewDidAppear 中:您可以设置动画并将视图带到起始坐标,例如 0。您可以按以下方式进行操作

      -(void)viewDidAppear:(BOOL)animated {
           [super viewDidAppear:animated];
      
           [UIView animateWithDuration:10.f animations:^{
                self.view.frame = CGRectMake(0.f, 0.f, 320.f, 568.f);
           }];
      }
      

    对于按钮,您必须具体。

    【讨论】:

    • 相对于您的实际视图设置框架可能会更好。 subview.frame = CGRectMake(0.f, 0.f, self.view.frame.size.width, self.view.frame.size.height);因为 568.0f 高度仅适用于 4" iPhone
    • 这只是一个假设动画概念很明确的例子!你可以阅读它的第一行。 :)
    【解决方案4】:

    从右到左的动画和从左到右的动画as-

            let firstViewTowardsLeft = UIView(frame: CGRect(x: 0, y: 150, width: 50, height: 50))
            firstViewTowardsLeft.backgroundColor = .red
            view.addSubview(firstViewTowardsLeft)
            UIView.animate(withDuration: 1, delay: 0, options: .repeat, animations: {Bool in
                firstViewTowardsLeft.frame = CGRect(x: 100, y: 150, width: 50, height: 50)
    
            }, completion: nil)
    
    
            let secondViewTowardsLeft = UIView(frame: CGRect(x: 100, y: 100, width: 50, height: 50))
            secondViewTowardsLeft.backgroundColor = .red
            view.addSubview(secondViewTowardsLeft)
            UIView.animate(withDuration: 1, delay: 0, options: .repeat, animations: {Bool in
                secondViewTowardsLeft.frame = CGRect(x: 0, y: 100, width: 50, height: 50)
    
            }, completion: nil)
    

    https://developer.apple.com/documentation/uikit/uiview/1622418-animate

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-30
      • 1970-01-01
      • 1970-01-01
      • 2016-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-04
      相关资源
      最近更新 更多