简体   繁体   English

在范围内变换UIView

[英]Transform UIView within bounds

I am trying to make a view transform and kind of have a circle effect until it reaches certain points then it just fills a rectangle. 我正在尝试进行视图转换,并使其具有圆形效果,直到达到特定点,然后才填充矩形。 This is for a material design project I am working on. 这是我正在从事的材料设计项目。 All the code is in Swift 2.0 on an iOS 8 or above device. 所有代码都在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)
    })

} }

Currently the view just grows beyond the size of the screen. 当前,视图仅会超出屏幕的大小。

The print statement returns -37176 instead of say 0. I want it to fill the screen and nothing more. 打印语句返回-37176而不是说0。我希望它填充屏幕,仅此而已。

If you want it to fill a rectangle. 如果要填充矩形。

1) create a rectangular container UIView. 1)创建一个矩形容器UIView。

2) add your rippleView as a subview to this rectangular uiview. 2)将您的rippleView作为子视图添加到此矩形uiview。

set the clipsToBounds property to yes of your container view. 将容器视图的clipsToBounds属性设置为yes。 and just do the animation. 然后做动画。

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);
}

Try in playground. 在操场上尝试。

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);
  • Copy this code in a playground File 将此代码复制到游乐场文件中
  • Show the Assistant editor 显示助理编辑
  • Press play (in the left bottom corner) 按播放(在左下角)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM