IOS基础002自定义控件
2015-08-05 22:55
316 查看
通过绘图api来 编写控件
1创建一个ProgressController.swift 继承UIView
private var _progressValue:CGFloat
public func getProgressValue()->CGFloat{
return _progressValue
}
public func setProgressValue(value:CGFloat){
_progressValue = value
setNeedDisplay()//刷新绘图
}
override func drawRect(rect:CGRect){
var ctx = UIGraphicsGetCurrentContext()//获取context
var r = rect.width/2
//获取半径
CGContextAddArc(context,r,r,r,0,3.141592653*2 ,0);画圆圈
CGContextSetRGBFillColor(ctx,0.7,0.7,0.7,1);//设置颜色
CGContextFillPath(ctx);//画图
CGContextAddArc(ctx,r,r,r,0,3.141592652*2*_progressValue)
CGContextSetRGBFillColor(ctx,1,0,1,1);//设置颜色
CGContextAddLineToPoint(,ctx,r,r)//连接圆弧与中点画线
CGContextFillPath(ctx)//画图
}
在ViewController里
var pc:ProgressCotrol
override func viewDidLoad(){
pc = ProgressCotrol(frame:CGRect(x:100,y:100,width:100,height:100))
pc.setProgressValue(0.2)
self.view.addSubView(pc)
}
设置一个按钮,按钮的点击事件
@IBAction func addProgressBtnProgressed(sender:AnyObject){
pc.setProgressValue(pc.getProgressValue()+0.1)
}
实现预览自定义控件
创建类MyView继承UIView
@IBDesignable class MyView :UIView 类要添加@IBDesignable 可设计的
@IBInspectable var str:Strin@IBInspectable var widthBorder:CGFloar = 0{ 属性使用@IBInspectable
didSet{ 在storyboard中设置属性的时候会调用didset里的代码
layer.borderWidth = widthBorder
}
}
IOS视图切换
在storyboard中添加两个view 设置图片 注:view与viewController在一层
在viewDidLoad(){
self.view.addSubview(img1) //添加img1
}
点击屏幕出发事件
override func touchesBegan(touches:NSSet!,widthEvent event:UIEvent!){
//运行后的回调函数
func completeHandler(value:Bool){
}
//动画效果 从img1翻转到img2
UIView.transitionFronView(img1,toView:img2,duration:1.0,
options:UIViewAnimationOptions.transitionFlipFromLeft,
completion:completeHandler)
}
翻页效果
override func touchesBegan(touches:NSSet!,withEvent event:UIEvent!){
UIView.beginAnimations(nil,context:nil)//开始动画
UIView.setAnimationTransition(UIViewAnimationTransition.CurlDown,
forView:img1,cache:true)
UIView.setAnimationDuration(1.0) //设置动画
UIView.commitAnimations()//启动动画
}
自定义动画
override func touchesBegan(touches:NSSet!,withEvent event:UIEvent){
func anim(){
img1.alpha = 0.5
img1.center = CGPoint(x:50,y:200)
}
func completeHandler(v:Bool){
println("complete")
}
UIView.transitionWithView(img1,duration:1.0,options:UIViewAnimationOptions.TransitionNone,
animations:anim,completion:completeHandler)
}
1创建一个ProgressController.swift 继承UIView
private var _progressValue:CGFloat
public func getProgressValue()->CGFloat{
return _progressValue
}
public func setProgressValue(value:CGFloat){
_progressValue = value
setNeedDisplay()//刷新绘图
}
override func drawRect(rect:CGRect){
var ctx = UIGraphicsGetCurrentContext()//获取context
var r = rect.width/2
//获取半径
CGContextAddArc(context,r,r,r,0,3.141592653*2 ,0);画圆圈
CGContextSetRGBFillColor(ctx,0.7,0.7,0.7,1);//设置颜色
CGContextFillPath(ctx);//画图
CGContextAddArc(ctx,r,r,r,0,3.141592652*2*_progressValue)
CGContextSetRGBFillColor(ctx,1,0,1,1);//设置颜色
CGContextAddLineToPoint(,ctx,r,r)//连接圆弧与中点画线
CGContextFillPath(ctx)//画图
}
在ViewController里
var pc:ProgressCotrol
override func viewDidLoad(){
pc = ProgressCotrol(frame:CGRect(x:100,y:100,width:100,height:100))
pc.setProgressValue(0.2)
self.view.addSubView(pc)
}
设置一个按钮,按钮的点击事件
@IBAction func addProgressBtnProgressed(sender:AnyObject){
pc.setProgressValue(pc.getProgressValue()+0.1)
}
实现预览自定义控件
创建类MyView继承UIView
@IBDesignable class MyView :UIView 类要添加@IBDesignable 可设计的
@IBInspectable var str:Strin@IBInspectable var widthBorder:CGFloar = 0{ 属性使用@IBInspectable
didSet{ 在storyboard中设置属性的时候会调用didset里的代码
layer.borderWidth = widthBorder
}
}
IOS视图切换
在storyboard中添加两个view 设置图片 注:view与viewController在一层
在viewDidLoad(){
self.view.addSubview(img1) //添加img1
}
点击屏幕出发事件
override func touchesBegan(touches:NSSet!,widthEvent event:UIEvent!){
//运行后的回调函数
func completeHandler(value:Bool){
}
//动画效果 从img1翻转到img2
UIView.transitionFronView(img1,toView:img2,duration:1.0,
options:UIViewAnimationOptions.transitionFlipFromLeft,
completion:completeHandler)
}
翻页效果
override func touchesBegan(touches:NSSet!,withEvent event:UIEvent!){
UIView.beginAnimations(nil,context:nil)//开始动画
UIView.setAnimationTransition(UIViewAnimationTransition.CurlDown,
forView:img1,cache:true)
UIView.setAnimationDuration(1.0) //设置动画
UIView.commitAnimations()//启动动画
}
自定义动画
override func touchesBegan(touches:NSSet!,withEvent event:UIEvent){
func anim(){
img1.alpha = 0.5
img1.center = CGPoint(x:50,y:200)
}
func completeHandler(v:Bool){
println("complete")
}
UIView.transitionWithView(img1,duration:1.0,options:UIViewAnimationOptions.TransitionNone,
animations:anim,completion:completeHandler)
}
相关文章推荐
- iOS开发--画一条黑色的横线
- iOS下OpenGL ES 3.0编程入门(一):构建Hello World环境
- 程序日志--ios“页面控制”程序
- iOS 8 AutoLayout与Size Class自悟
- [转载] iOS开发 - 封装文件上传工具类
- wxhl ios bj 49期 张浩 学习心得体会连载 第四章
- iOS在照片上添加水印
- ios状态栏的一些操作
- IOS内存管理--手动引用计数实现
- 小白学开发(iOS)OC_ 手动内存管理类的相互引用问题(2015-08-05)
- [ios] How to fix thread 1 signal sigabrt
- 小白学开发(iOS)OC_ @property 参数(2015-08-05)
- 【iOS知识学习】_iOS动态改变TableView Cell高度
- 小白学开发(iOS)OC_ set方法的内存管理分析(2015-08-04)
- IOS不同层的传输格式
- 小白学开发(iOS)OC_多个对象的内存管理(2015-08-03)
- OC_NSData
- 小白学开发(iOS)OC_ 单个对象的内存管理(2015-08-02)
- iOS开发之---将时间戳,转化成时间字符串。
- 小白学开发(iOS)OC_手动内存管理(2015-08-02)