如何用Swift创建qt 自定义控件iOS控件

发送私信成功
DevStore用户登录
还没有DevStore帐号?
快捷登录:
您目前的活力值不够下载该资源哦~~
怎么样快速获得活力值?
下载此资源将扣除活力值-20
(只在首次下载扣除活力值,之后可以免费下载)
满足所有需求,助您轻松工作
> 源码详情
用swift做的可以创建表格、自定义外观的库
11:15 && 浏览量(1377) &&
功能分类:工具
支持平台:iOS
运行环境:iOS
开发语言:swift
开发工具:Xcode
源码大小:2.70MB
105 人下载
用Swift编写的一个强大且非常灵活的库,可用来简单地创建表格,可自定义单元格外观。可使用自定义单元格并定义你自己的选择器控件。
DevStore所有源码来自用户上传分享,版权问题及牵扯到商业纠纷均与DevStore无关。
let form = FormDescriptor()
form.title = "Example form"
let section1 = FormSectionDescriptor()
var row: FormRowDescriptor! = FormRowDescriptor(tag: Static.nameTag, rowType: .Email, title: "Email")
section1.addRow(row)
row = FormRowDescriptor(tag: Static.passwordTag, rowType: .Password, title: "Password")
section1.addRow(row)
let section2 = FormSectionDescriptor()
row = FormRowDescriptor(tag: Static.nameTag, rowType: .Text, title: "Name")
section2.addRow(row)
row = FormRowDescriptor(tag: Static.lastNameTag, rowType: .Text, title: "Last name")
section2.addRow(row)
let section3 = FormSectionDescriptor()
row = FormRowDescriptor(tag: Static.URLTag, rowType: .URL, title: "URL")
section3.addRow(row)
row = FormRowDescriptor(tag: Static.phoneTag, rowType: .Phone, title: "Phone")
section3.addRow(row)
let section4 = FormSectionDescriptor()
row = FormRowDescriptor(tag: Static.enabled, rowType: .BooleanSwitch, title: "Enable")
section4.addRow(row)
row = FormRowDescriptor(tag: Static.check, rowType: .BooleanCheck, title: "Doable")
section4.addRow(row)
row = FormRowDescriptor(tag: Static.segmented, rowType: .SegmentedControl, title: "Priority")
row.options = [0, 1, 2, 3]
row.titleFormatter = { value in
switch( value ) {
return "None"
return "!"
return "!!"
return "!!!"
return nil
row.cellConfiguration = ["titleLabel.font" : UIFont.boldSystemFontOfSize(30.0), "segmentedControl.tintColor" : UIColor.redColor()]
section4.addRow(row)
section4.headerTitle = "An example header title"
section4.footerTitle = "An example footer title"
下载(105)
获取活力值
源码上传作者
资料下载排行
开发者交流群:
DevStore技术交流群2:
运营交流群:
产品交流群:
深圳尺子科技有限公司
深圳市南山区蛇口网谷万海大厦C栋504
Copyright (C) 2015 DevStore. All Rights Reservedios-swift 中自定义控件的实现 - 简书
ios-swift 中自定义控件的实现
先讲一下思路我们继承UIView写一个LTView,用UILabel和UITextField作为LTView的子View.1.首先创建一个新文件,选择xcode菜单,File -& New -& File
2.得到文件LTView.swift,我们给LTView创建两个属性,并对其初始化法
3.这时会出现一个系统能够自动修改的 错误,所以我们利用系统的错误修正功能对其自行修正,会自动补上下列代码required init?(coder aDecoder: NSCoder) {fatalError("init(coder:) has not been implemented")}4.下面我们写布局一个子视图的方法setupSubView,初始化label和textFieldfunc setupSubView(){//初始化这两个属性label和textFieldself.label = UILabel(frame: CGRect(x: 0, y: 0, width: self.frame.size.width*0.2, height: self.frame.size.height))self.label.backgroundColor = #colorLiteral(red: 0., green: 0., blue: 0., alpha: 1)//设置文字居中self.label.textAlignment = .center//切圆角self.label.layer.cornerRadius = 5self.label.clipsToBounds = trueself.addSubview(label)//添加到LTView上self.textField = UITextField(frame: CGRect(x: self.frame.size.width*0.2+10, y: 0, width: self.frame.size.width*0.8-10, height: self.frame.size.height))//定义圆角模式self.textField.borderStyle = .roundedRect//设置密闻输入self.textField.isSecureTextEntry = trueself.textField.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)self.addSubview(textField)}4.回到 初始化方法中,对新定义的setupSubView方法进行调用override init(frame: CGRect) {//调用父类对这个方法的实现super.init(frame: frame)self.setupSubView() //调用子视图方法}5.进入AppDelegate.swift中class AppDelegate: UIResponder, UIApplicationDelegate ,UITextFieldDelegate{//UITextFieldDelegate代理需要遵守的协议var window: UIWindow?//定义为全局的变量var textField:UITextField!var textField2:UITextField!func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -& Bool {self.window = UIWindow(frame: UIScreen.main.bounds)self.window?.backgroundColor = #colorLiteral(red: 0., green: 0., blue: 0., alpha: 1)self.window?.makeKeyAndVisible()self.window?.rootViewController = ViewController()let image = UIImageView(image: UIImage(named: "girl2.jpg"))image.frame = CGRect(x: 100, y: 50, width: 200, height: 200)//对图片进行切圆角处理image.layer.cornerRadius = 100;image.clipsToBounds = trueself.window?.addSubview(image)let aLTView = LTView(frame: CGRect(x: 7, y: 300, width: 400, height: 50))aLTView.label.text = "用户名"//文本对齐格式,textAlignment是一个枚举,所以可以省略类型名//aLTView.label.textAlignment = NSTextAlignment.centeraLTView.label.textAlignment = .centeraLTView.textField.placeholder = "请输入用户名"aLTView.textField.delegate = selfself.window?.addSubview(aLTView)let bLTView = LTView(frame: CGRect(x: 7, y: 370, width: 400, height: 50))bLTView.label.text = "密码"//提示字符bLTView.textField.placeholder = "请输入密码"//设置代理bLTView.textField.delegate = selfself.window?.addSubview(bLTView)let registerButton = UIButton(frame: CGRect(x: 80, y: 500, width: 100, height: 50))registerButton.backgroundColor = UIColor.grayregisterButton.setTitle("登录", for: .normal)self.window?.addSubview(registerButton)let cancelButton = UIButton(frame: CGRect(x: 220, y: 500, width: 100, height: 50))cancelButton.backgroundColor = UIColor.graycancelButton.setTitle("取消", for: .normal)self.window?.addSubview(cancelButton)textField = aLTView.textFieldtextField2 = bLTView.textFieldreturn true}//点击return收起键盘
func textFieldShouldReturn(_ textField: UITextField) -& Bool {
//第一响应事件
textField.resignFirstResponder()
return true
//点击空白处收起键盘
override func touchesBegan(_ touches: Set, with event: UIEvent?) {textField.resignFirstResponder()textField2.resignFirstResponder()}func applicationWillResignActive(_ application: UIApplication) {}func applicationDidEnterBackground(_ application: UIApplication) {}func applicationWillEnterForeground(_ application: UIApplication) {}func applicationDidBecomeActive(_ application: UIApplication) {}func applicationWillTerminate(_ application: UIApplication) {}}最终效果如何使用Swift开发iOS应用时实现手势识别完整教程 - 吕滔博客
如何使用Swift开发iOS应用时实现手势识别完整教程
&7480&次浏览
在这次IOS应用开发教程中,我们打算实现手势识别。正如你所知道的,IOS支持大量的手势操作,它们能提供了很好的应用控制和出色用户体验。
让我们开始吧!
首先需要在Xcode中创建一个新的Single View Application:
然后点击Next,弹出的窗口要求你填写项目设置。在第一栏 (“Product name”) 中填入项目名称后,点击Next.
确保语言选择的是 “Swift”.
点击 “Main.storyboard” 文件,拖出6个 UIViews放到视图中.把视图排列成如图所示的样子.当你排列UIViews时,在每个view下面添加一个UILabel并依图设定文本值。
我们开始写代码吧.
是时候编辑实现文件了 (在我们的案例 “ViewController.swift” ).
为了声明一些我们将会用到的变量,要在 “class ViewController: UIViewController“块中添加如下代码.class ViewController: UIViewController {
@IBOutlet var tapView: UIView
@IBOutlet var swipeView: UIView
@IBOutlet var longPressView: UIView
@IBOutlet var pinchView: UIView
@IBOutlet var rotateView: UIView
@IBOutlet var panView: UIView
var lastRotation = CGFloat()
let tapRec = UITapGestureRecognizer()
let pinchRec = UIPinchGestureRecognizer()
let swipeRec = UISwipeGestureRecognizer()
let longPressRec = UILongPressGestureRecognizer()
let rotateRec = UIRotationGestureRecognizer()
let panRec = UIPanGestureRecognizer()
}在第2 – 7行,我们声明了在之前界面里排列过的 UIViews.
在第8行,我们声明了实现旋转手势要用到的变量(lastRotation).
在第 9 – 14行,我们为每个view声明了一个手势识别对象.
注意:&在 Swift中,我们用let关键字声明常量,这意味着它的值在程序运行时不可改变。关键字var则声明普通变量。
当声明完应用需要的主要变量后,在viewDidLoad&方法中添加如下代码.override func viewDidLoad() {
super.viewDidLoad()
tapRec.addTarget(self, action: "tappedView")
pinchRec.addTarget(self, action: "pinchedView:")
swipeRec.addTarget(self, action: "swipedView")
longPressRec.addTarget(self, action: "longPressedView")
rotateRec.addTarget(self, action: "rotatedView:")
panRec.addTarget(self, action: "draggedView:")
tapView.addGestureRecognizer(tapRec)
swipeView.addGestureRecognizer(swipeRec)
pinchView.addGestureRecognizer(pinchRec)
longPressView.addGestureRecognizer(longPressRec)
rotateView.addGestureRecognizer(rotateRec)
panView.addGestureRecognizer(panRec)
rotateView.userInteractionEnabled = true
rotateView.multipleTouchEnabled = true
pinchView.userInteractionEnabled = true
pinchView.multipleTouchEnabled = true
tapView.userInteractionEnabled = true
swipeView.userInteractionEnabled = true
longPressView.userInteractionEnabled = true
panView.userInteractionEnabled = true
}第 3 – 8行,为每个视图设定手势识别的目标。所谓的目标,就是每个view中的手势完成后要调用的方法。
第 9 -14行,把手势识别添加到视图中.
第15 – 22行,把每个视图的&userInteractionEnabled属性设为ture,并把拥有需要多点触控(rotateView&and&pinchView)的手势所在的视图的multipleTouchEnabled属性设为true.
现在,我们编写每个手势识别器要调用的方法 (第3 – 8行设置的目标方法 ).
添加如下代码:func tappedView(){
let tapAlert = UIAlertController(title: "Tapped", message: "You just tapped the tap view", preferredStyle: UIAlertControllerStyle.Alert)
tapAlert.addAction(UIAlertAction(title: "OK", style: .Destructive, handler: nil))
self.presentViewController(tapAlert, animated: true, completion: nil)
func swipedView(){
let tapAlert = UIAlertController(title: "Swiped", message: "You just swiped the swipe view", preferredStyle: UIAlertControllerStyle.Alert)
tapAlert.addAction(UIAlertAction(title: "OK", style: .Destructive, handler: nil))
self.presentViewController(tapAlert, animated: true, completion: nil)
func longPressedView(){
let tapAlert = UIAlertController(title: "Long Pressed", message: "You just long pressed the long press view", preferredStyle: UIAlertControllerStyle.Alert)
tapAlert.addAction(UIAlertAction(title: "OK", style: .Destructive, handler: nil))
self.presentViewController(tapAlert, animated: true, completion: nil)
}这三种方法都很好地完成同一件事.每次在手势在相应的视图中完成后,每种方法都弹出一个对话框.
所以&tappedView()方法在用户滑动视图时弹出一个对话框,swipedView()&方法在用户触摸滑动 swipe视图时弹出对话框,而longPressedView()&方法则在用户长按long press view时弹出对话框.
另两种手势 (rotate and pinch ) 的代码稍微有点复杂.
为旋转手势添加如下代码:func rotatedView(sender:UIRotationGestureRecognizer){
var lastRotation = CGFloat()
self.view.bringSubviewToFront(rotateView)
if(sender.state == UIGestureRecognizerState.Ended){
lastRotation = 0.0;
rotation = 0.0 - (lastRotation - sender.rotation)
var point = rotateRec.locationInView(rotateView)
var currentTrans = sender.view.transform
var newTrans = CGAffineTransformRotate(currentTrans, rotation)
sender.view.transform = newTrans
lastRotation = sender.rotation
}这个方法包含&sender:UIRotationGestureRecognizer参数. sender 参数(&UIRotationGestureRecognizer&类型) 含有这个方法(在这个案例中是rotateRec)调用的手势识别器的值.
第2行声明了&lastRotation.
第3行我们把&rotateView放到前面.
接下来,在 if语句中,我们检查手势是否完成,如果没有完成,我们就将视图旋转。
第 8 – 10行,我们计算rotate view的旋转程度,第10行,我们设置rotate view的旋转程度。
On line 12 we set thelastRotation&作为旋转手势识别器的当前旋转.
现在我们添加pinch 手势的代码:func pinchedView(sender:UIPinchGestureRecognizer){
self.view.bringSubviewToFront(pinchView)
sender.view.transform = CGAffineTransformScale(sender.view.transform, sender.scale, sender.scale)
sender.scale = 1.0
}在之前方法的第1行中,我们把pinch视图放到了顶端。然后设置每个pinch视图的transform,并把pinchRec的scale设为1.
然后是实现 pan (drag) 手势.&添加如下代码:func draggedView(sender:UIPanGestureRecognizer){
self.view.bringSubviewToFront(sender.view)
var translation = sender.translationInView(self.view)
sender.view.center = CGPointMake(sender.view.center.x + translation.x, sender.view.center.y + translation.y)
sender.setTranslation(CGPointZero, inView: self.view)
}第2行,我们把 drag视图放到顶端 (和前面的方法一样).
然后我们声明变量translation,并用sender.translationInView(self.view)的值给它赋值。 完成后,把sender.view object (panRec)&的center属性设为计算出来的新center &( 通过CGPointMake(sender.view.center.x + translation.x, sender.view.center.y + translation.y)&计算) 并把translation 设为 sender (panRec).
现在,代码部分算是完成了!
回到界面设计.
现在我们回到 “Main.storyboard” 文件. 选择视图控制器并把声明的每个UIView连接到相应的视图,如下图所示.
现在你可以在模拟器或你的设备上运行该应用并测试手势。
附源代码:
扫一扫支付
评论数: 27
评论数: 20
评论数: 19
评论数: 14
评论数: 11
评论数: 10
评论数: 9
评论数: 8
评论数: 8
评论数: 7
Hello! This post could not be w...
你好,yaf怎么使用循环输出数据到模板中
为什么我的没有mvn命令,而且setting.xml文件是在 ...
请问pid怎么用
请问你这个pid是哪里的
一般台式机速度从来都没有低于100MB/S,服务器做RAID5...
牛人,学习了,致敬。
lvm2-monitor
0:off 1:on 2:on...
你好我想问问,我启用插件后 就显示Server Error ,...
最厉害的方法
评论数: 0
评论数: 1
评论数: 0
评论数: 0
评论数: 0
评论数: 0
评论数: 0
评论数: 0
评论数: 0
评论数: 0}

我要回帖

更多关于 安卓 创建自定义控件 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信