在UITouch事件中画圆圈-iOS8 Swift基础教程
这篇教程主要内容展示如何利用Core Graphics Framework画圆圈,当用户点击屏幕时随机生成不同大小的圆,这篇教程在Xcode6和iOS8下编译通过。
打开Xcode,新建项目选择Single View Application,Product Name填写iOS8SwiftDrawingCirclesTutorial,Organization Name和Organization Identifier根据自己填写,选择Swift语言与iPhone设备。
File->New File->iOS->Source -> CocoTouch Class.选择swift 语言,创建继承于UIView
的CirleView
类,如下图
在CircleView
中增加如下init 方法:
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.clearColor()
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
将cirecle view的背景颜色清除掉,这样多个圆圈可以相互重叠在一起,下面实现drawRect方法:
override func drawRect(rect: CGRect) {
// Get the Graphics Context
var context = UIGraphicsGetCurrentContext();
// Set the circle outerline-width
CGContextSetLineWidth(context, 5.0);
// Set the circle outerline-colour
UIColor.redColor().set()
// Create Circle
CGContextAddArc(context, (frame.size.width)/2, frame.size.height/2, (frame.size.width - 10)/2, 0.0, CGFloat(M_PI * 2.0), 1)
// Draw
CGContextStrokePath(context);
}
在drawRect
方法中,我们将圆圈的边框线设置为5并居中显示,最后调用CGContextStrokePath
画出圆圈.现在我们打开ViewController.swift文件,在viewDidLoad
方法中将背景颜色设置为ligthgray.
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.lightGrayColor()
}
现在当用户点击屏幕时我们需要创建一个cirecle view,接下来在ViewController.swift中重载touchesBegan:withEvent方法中实现
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
// loop through the touches
for touch in touches {
// Set the Center of the Circle
// 1
var circleCenter = touch.locationInView(view)
// Set a random Circle Radius
// 2
var circleWidth = CGFloat(25 + (arc4random() % 50))
var circleHeight = circleWidth
// Create a new CircleView
// 3
var circleView = CircleView(frame: CGRectMake(circleCenter.x, circleCenter.y, circleWidth, circleHeight))
view.addSubview(circleView)
}
}
- 1.circle圆圈设置在用户的点击位置
- 2.circle高度与宽度随机产生,数值在25-75之间
- 3.创建circleView并添加至main view中
编译运行项目后,点击屏幕可以看到类似如下效果:
原文:http://www.ioscreator.com/tutorials/drawing-circles-uitouch-ios8-swift
在UITouch事件中画圆圈-iOS8 Swift基础教程的更多相关文章
- iOS8使用Core Graphics实现渐变效果-Swift基础教程
Core Graphics是一个强大的底层API,在这篇教程中我们主要使用Core Graphics来实现渐变效果,为了简单起见,我们采用线性渐变.线性渐变是从起点到终点颜色进行顺序渐变.教程在iOS ...
- swift基础教程笔记
http://www.imooc.com/learn/127 <玩儿转swift> 慕课网教程笔记,自己根据2.1的语法做了更新. I. 1.通过playground来学习.熟悉swift ...
- Swift基础--通知,代理和block的使用抉择以及Swift中的代理
什么时候用通知,什么时候用代理,什么时候用block 通知 : 两者关系层次太深,八竿子打不着的那种最适合用通知.因为层级结构深了,用代理要一层一层往下传递,代码结构就复杂了 代理 : 父子关系,监听 ...
- python中画散点图
python中画散点图 示例代码: import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d impor ...
- 在Application_Error事件中获取当前的Action和Control
ASP.NET MVC程序处理异常时,方法有很多,网上也有列举了6种,下面是使用全局处理在Global.asax文件的Application_Error事件中实现.既然是ASP.NET MVC,我需要 ...
- 用字体在网页中画Icon图标
第一步,下载.IcoMoon网站选择字体图标并下载,解压后将fonts文件夹放在工程目录下.fonts文件夹内有四种格式的字体文件: 注:由于浏览器对每种字体的支持程度不一致,要想在所有浏览器中都显示 ...
- touch事件中的touches、targetTouches和changedTouches详解
touches: 当前屏幕上所有触摸点的列表; targetTouches: 当前对象上所有触摸点的列表; changedTouches: 涉及当前(引发)事件的触摸点的列表 通过一个例子来区分一下触 ...
- 字体在网页中画ICON图标
用字体在网页中画ICON图标有三种小技巧: 1.用CSS Sprite在网页中画小图标 实现方法: 首先将小图片整合到一张大的图片上 然后根据具体图标在大图上的位置,给背景定位.background- ...
- 功能源代码(扇形进度)及Delegate运用在开放事件中、UINavigationController的封装
1:扇形进度视图及运用 首先先创建扇形的视图,传入进度值 #import <UIKit/UIKit.h> @interface LHProgressView : UIView @prope ...
随机推荐
- iOS 10 因苹果健康导致闪退 crash
如果在app中调用了苹果健康,iOS10中会出现闪退.控制台报出的原因是: Terminating app due to uncaught exception 'NSInvalidArgumentEx ...
- strstr 的使用
Problem E: Automatic Editing Source file: autoedit.{c, cpp, java, pas} Input file: autoedit.in Outpu ...
- ANTLR3
ANother Tool for Language Recognition start...
- checkbox、select、radio的设置与获取
参考链接:http://www.cnblogs.com/xiaopin/archive/2011/09/13/2175190.html js版本: <!DOCTYPE html PUBLIC & ...
- JSP中的相对路径和绝对路径(转)
1.首先明确两个概念: 服务器路径:形如:http://192.168.0.1/的路径 Web应用路径:形如:http://192.168.0.1/yourwebapp的路径 2.关于相对路径与绝对路 ...
- weblogic的ejb远程调用
这是一篇对EJB远程调用的简单范例. 1.环境:win7 + weblogic 12c + myeclipse8.5 2.目的:实现在myeclispe中对weblogic中EJ ...
- C#--遍历目录实例
鉴于前面几篇博客都说了,这边就啥都不说了.直接就開始贴代码了. 1.控件解释: FolderBrowserDialog控件一个----用来显示"浏览目录"对话框 TextBox控件 ...
- CSS3线性渐变linear-gradient
转自 http://www.w3cplus.com/content/css3-gradient CSS3的线性渐变 一.线性渐变在Mozilla下的应用 -moz-linear-gradient( [ ...
- HTML5.1就要来了
原文来自https://www.w3.org/blog/2016/04/working-on-html5-1/ 总结一下几个点: 1.六个月内,也就是到九月份的时候,HTML5.1会和大家见面. 2. ...
- IntelliJ IDEA 14 注册码生成器
IntelliJ IDEA 14 注册码生成器 文件为Java代码 自己编译运行里面的程序输入名称然后就生成注册码了工具:http://yun.baidu.com/s/1cZKsA部分工具生成的注册码 ...