【问题标题】:iOS: zooming an image with a buttoniOS:使用按钮缩放图像
【发布时间】:2013-05-07 21:17:49
【问题描述】:

我有一个 imageView 作为应用程序的背景。我在背景顶部有一个按钮。我想按下该按钮并放大背景图像的动画。解决此问题的最佳方法是什么?我应该使用滚动视图吗?

【问题讨论】:

    标签: ios xcode zooming scrollview


    【解决方案1】:

    我认为在动画块中使用变换是最好的方法。 你可以这样做:

    [UIView animateWithDuration:0.3 animations:^
    {
        [yourImageView setTransform:CGAffineTransformMakeScale(1.5, 1.5)];
    }];
    

    并将其放回初始位置,您只需要这样做:

    [UIView animateWithDuration:0.3 animations:^
    {
        [yourImageView setTransform:CGAffineTransformIdentity];
    }];
    

    【讨论】:

      【解决方案2】:

      这样的?

      UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
      myButton.frame = CGRectMake(80, 50, 150, 40);
      [myButton setTitle:@"Your button!" forState:UIControlStateNormal];
      [myButton setBackgroundImage:[UIImage imageNamed:@"xyz.png"]];     
      [self.view addSubview:myButton];
      
      -(IBAction)buttonClicked:(UIButton *)sender
      {
         self.frame = CGRectMake(0.0f, 0.0f, 200.0f, 150.0f);
         [UIView beginAnimations:@"Zoom" context:NULL];
         [UIView setAnimationDuration:0.5];
         self.frame = CGRectMake(0.0f, 0.0f, 1024.0f, 768.0f);
         [UIView commitAnimations];
      }
      

      【讨论】:

      • 在action方法中不要启动动画,会和按钮动画发生碰撞。将它发布到一个块中的主队列,它看起来会好很多。不过答案很好!
      【解决方案3】:
      swift 4
      
          func zoomOut(){
          DispatchQueue.main.async{[weak self] in
              UIView.animate(withDuration: 0.3, animations: {[weak self] in
                  self?.imageView.transform = .identity
              })
          }
      }
      
      func zoomIn(){
          DispatchQueue.main.async{[weak self] in
              UIView.animate(withDuration: 0.3, animations: {[weak self] in
                  self?.imageView.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
              })
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-19
        • 2012-09-24
        • 2020-07-30
        • 1970-01-01
        • 2020-01-09
        • 2017-01-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多