gpt4 book ai didi

ios - 在边界内转换 UIView

转载 作者:搜寻专家 更新时间:2023-11-01 07:30:33 24 4
gpt4 key购买 nike

我正在尝试进行 View 变换并产生一种圆形效果,直到它到达某些点然后它才填充一个矩形。这是我正在从事的 Material 设计项目。所有代码都在 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)
})

目前 View 刚好超出屏幕大小。

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

最佳答案

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

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

2) 将您的 rippleView 作为 subview 添加到此矩形 uiview。

将容器 View 的 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);
}

在 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);
  • 将此代码复制到 playground 文件中
  • 显示助理编辑
  • 按播放(在左下角)

关于ios - 在边界内转换 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32682829/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com