【问题标题】:Transform UIView within bounds在边界内转换 UIView
【发布时间】:2015-09-20 18:29:39
【问题描述】:

我正在尝试进行视图变换,并且在达到某些点之前具有圆形效果,然后它只会填充一个矩形。这是我正在从事的材料设计项目。所有代码都在 iOS 8 或以上设备上的 Swift 2.0 中。

func helloWorld(sender: UIButton) {
    let point = sender.frame.origin
    let rippleViewInitFrame: CGRect = CGRect(x: point.x, y: point.y, width: 4, height: 4)
    let rippleView: UIView = UIView(frame: rippleViewInitFrame)
    rippleView.backgroundColor = UIColor.MDColor.blue
    let bounds: CGRect = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
    rippleView.layer.masksToBounds = true
    rippleView.layer.cornerRadius = 2
    self.view.addSubview(rippleView)
    UIView.animateWithDuration(0.5, delay: 0.0, options: .CurveEaseInOut, animations: {
        rippleView.transform = CGAffineTransformMakeScale(200.0, 200.0)
        rippleView.bounds = bounds

        }, completion: {
            finished in
            print(rippleView.frame.origin.x)
    })

}

目前视图刚刚超出屏幕大小。

打印语句返回 -37176 而不是 0。我希望它填满屏幕,仅此而已。

【问题讨论】:

    标签: ios swift animation uiview swift2


    【解决方案1】:

    如果你想让它填充一个矩形。

    1) 创建一个矩形容器UIView。

    2) 将您的波纹视图作为子视图添加到此矩形 uiview。

    将您的容器视图的 clipsToBounds 属性设置为 yes。 然后做动画。

    let rectView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100));
    rectView.backgroundColor = UIColor.redColor()
    
    // initialRippleView
    let rippleView = UIView(frame: CGRect(x: 15, y: 15, width: 2, height: 2));
    rippleView.backgroundColor = UIColor.whiteColor()
    rippleView.cornerRadius = rippleView.width / 2;
    
    rectView.addSubview(rippleView);
    
    rectView.clipsToBounds = true;
    UIView.animateWithDuration(5) { () -> Void in
        rippleView.transform = CGAffineTransformMakeScale(100, 100);
    }
    

    在操场上试试。

    import UIKit
    import XCPlayground
    
    
    // the main View
    let iPhone = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 568));
    iPhone.backgroundColor = UIColor.greenColor();
    
    // the rect View that will be the bounds of the animation
    let rectView = UIView(frame: CGRect(x: 0, y: 0, width: 150, height: 150));
    rectView.backgroundColor = UIColor.redColor()
    rectView.center = iPhone.center;
    // initialRippleView
    let rippleView = UIView(frame: CGRect(x: 15, y: 15, width: 2, height: 2));
    rippleView.layer.cornerRadius = 1;
    
    rippleView.backgroundColor = UIColor.whiteColor()
    
    iPhone.addSubview(rectView);
    rectView.addSubview(rippleView);
    // this property clips the drawings of subview to be clipped to the bounds of rectView. 
    rectView.clipsToBounds = true;
    UIView.animateWithDuration(5) { () -> Void in
        // you may need to calculate the right scale factor
        rippleView.transform = CGAffineTransformMakeScale(200, 200);
    }
    
    // Playground stuff
    XCPShowView("Container View", view: iPhone);
    
    • 将此代码复制到 Playground 文件中
    • 显示助理编辑器
    • 按播放(在左下角)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-02
      • 2011-11-08
      • 1970-01-01
      • 2021-05-19
      • 2010-11-10
      相关资源
      最近更新 更多