IOS5 2D绘图 -- CGColor

1、保存与释放color对象

void CGColorRelease (CGColorRef color); -- 递减Quartz color的retain总数。此函数等效于CFRelease,除非color为NULL而产生的错误。


CGColorRef CGColorRetain (CGColorRef color); -- 递增Quartz color的retain总数。此函数等效于CFRelease,除非color为NULL而产生的错误。


2、创建Quartz color对象


CGColorRef CGColorCreate (CGColorSpaceRef colorspace,const CGFloat components[]); --  利用一系列强度(包括透明度)和颜色空间创建一个Quartz color并有责任用CGColorRelease释放这个对象。


CGColorRef CGColorCreateCopy (CGColorRef color);  ---  创建一个已经存在的Quartz color拷贝并有责任用CGColorRelease释放这个对象。


CGColorRef CGColorCreateCopyWithAlpha (CGColorRef color,CGFloat alpha);  ---  创建一个已经存在的Quartz color拷贝,重设一个新的透明值并有责任用CGColorRelease释放这个对象。


CGColorRef CGColorCreateWithPattern (CGColorSpaceRef colorspace,CGPatternRef pattern,const CGFloat components[]);  ---  创建一个Quartz color使用一系列强度(包含透明值),颜色空间模式和模式。并有责任用CGColorRelease释放这个对象。



3、获取Quartz color相关说明


bool CGColorEqualToColor (CGColorRef color1,CGColorRef color2);  ---  判断2种颜色是否相同。如果2个颜色的颜色空间和数种颜色组成都相同则这2种颜色相同。


CGFloat CGColorGetAlpha (CGColorRef color);  --- 返回一个Quartz Color相关联的透明值。透明强度值区间在[0,1]之间,这个值代表不透明值


CGColorSpaceRef CGColorGetColorSpace (CGColorRef color); ---  返回一个Quartz color相关联的颜色空间值,如有需要,有责任对返回值作出保存和释放操作。


const CGFloat * CGColorGetComponents (CGColorRef color);  ---  返回一个Quartz color相关联的颜色组成(包括透明度)。


size_t CGColorGetNumberOfComponents (CGColorRef color); --- 返回一个Quartz color相关联的颜色组成成员数量。


CGPatternRef CGColorGetPattern ( CGColorRef color );  ---  返回一个Quartz color相关联的模式。

CFTypeID CGColorGetTypeID ( void );  --- 为Quartz

Returns the Core Foundation type identifier for a Quartz color data type. ---  返回核心基础类型标识Quartz color数据类型。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个手势解锁的Demo,你可以参考一下: ``` import UIKit class GestureLockViewController: UIViewController { // MARK: - Properties var buttons = [UIButton]() var selectedButtons = [UIButton]() var lines = [CAShapeLayer]() var touchPoint: CGPoint? var isTouching = false // MARK: - Lifecycle override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white let margin: CGFloat = 40 let distance: CGFloat = 80 let buttonWidth: CGFloat = 60 let buttonHeight: CGFloat = 60 let viewWidth = view.bounds.width let viewHeight = view.bounds.height for i in 0..<9 { let row = CGFloat(i / 3) let col = CGFloat(i % 3) let x = margin + col * (buttonWidth + distance) let y = margin + row * (buttonHeight + distance) let button = UIButton(frame: CGRect(x: x, y: y, width: buttonWidth, height: buttonHeight)) button.layer.cornerRadius = buttonWidth / 2 button.layer.borderWidth = 2 button.layer.borderColor = UIColor.lightGray.cgColor button.tag = i view.addSubview(button) buttons.append(button) } } // MARK: - Gesture Methods override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { guard let touch = touches.first else { return } let point = touch.location(in: view) for button in buttons { if button.frame.contains(point) && !selectedButtons.contains(button) { touchPoint = button.center selectedButtons.append(button) isTouching = true break } } } override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { guard isTouching, let touch = touches.first else { return } let point = touch.location(in: view) touchPoint = point for button in buttons { if button.frame.contains(point) && !selectedButtons.contains(button) { button.isSelected = true selectedButtons.append(button) let line = CAShapeLayer() line.strokeColor = UIColor.gray.cgColor line.fillColor = UIColor.clear.cgColor line.lineWidth = 3 view.layer.addSublayer(line) lines.append(line) break } } drawLines() } override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { isTouching = false touchPoint = nil for button in buttons { button.isSelected = false } validatePassword() clearSelectedButtons() clearLines() } override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) { isTouching = false touchPoint = nil for button in buttons { button.isSelected = false } clearSelectedButtons() clearLines() } // MARK: - Private Methods private func drawLines() { guard let point = touchPoint else { return } let linePath = UIBezierPath() linePath.move(to: point) for button in selectedButtons { linePath.addLine(to: button.center) } if let lastButton = selectedButtons.last, isTouching { linePath.addLine(to: lastButton.convert(lastButton.center, to: view)) } lines.last?.path = linePath.cgPath } private func clearSelectedButtons() { for button in selectedButtons { button.isSelected = false } selectedButtons.removeAll() } private func clearLines() { for line in lines { line.removeFromSuperlayer() } lines.removeAll() } private func validatePassword() { let password = selectedButtons.map { "\($0.tag)" }.joined() print("Gesture password: \(password)") } } ``` 这个Demo实现了一个3x3的手势解锁界面,使用了`UIButton`和`CAShapeLayer`来实现。当用户滑动手指时,会根据手指位置,自动连接之前选中的按钮,形成一条线。当用户抬起手指时,会根据选中的按钮的顺序,输出一个密码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值