这里都是基于对View的属性和方法进行学习,注意与OC的对比,另外其他的属性和方法看他们API接口就好了
一、UIView
public init(frame: CGRect)
public init?(coder aDecoder: NSCoder)
// 注意比较两种初始化的比较
let view0:UIView! = UIView()
let view1:UIView! = UIView.init(frame:CGRectMake(20, 20, 100, 100))
//这是为什么呢,下面哪一个好呢
view0.frame = CGRectMake(20, 20, 100, 100)
view1.frame = CGRect(origin: CGPoint(x:20,y:200), size: CGSize(width:100, height: 100))
// 常用属性,与OC比较又有什么不同呢
view1.backgroundColor = UIColor.redColor()
view1.alpha = 0.5
view1.tag = 10001
view1.layer.masksToBounds = true // YES
view1.layer.cornerRadius = 5.0
// 常用方法
view0 .removeFromSuperview()
view1 .addSubview(view0)
view1 .bringSubviewToFront(view0)
view0 .layoutIfNeeded()
// 加载父视图
self.view.addSubview(view0)
self.view.addSubview(view1)
二、UILabel
let label0 : UILabel = UILabel()
label0.frame = CGRect(origin: CGPoint(x: 20, y: 150), size: CGSize(width: 300, height: 40))
// 注意写法的不同
label0.backgroundColor = UIColor.blackColor()
// label0.textAlignment = NSTextAlignment.Center
label0.textAlignment = .Center // 也可以这样写
label0.text = "testLabel,there are have many we need learn,go ahead,you are the best man,we have a dream,we should have a dream "
label0.font = UIFont.systemFontOfSize(20)
label0.textColor = UIColor.greenColor()
label0.shadowColor = UIColor.grayColor()
// 偏移量 左右不变,像下偏移1,是默认的偏移量
label0.shadowOffset = CGSizeMake(0, 1)
// 让字体适应宽度
// label0.adjustsFontSizeToFitWidth = true
// 可以多行
label0.numberOfLines = 0
// 当字数多的时候的处理方式,显示那个 ... 以及位置,或者不显示
// label0.lineBreakMode = NSLineBreakMode.ByClipping
// 显示使用缩小文本位置基于最初的基线,后期可能会用的多
// label0.baselineAdjustment = UIBaselineAdjustment.AlignCenters
// iOS 9 新加的,默认是False,设置true 会让字体之间更紧凑一些
label0.allowsDefaultTighteningForTruncation = false
self.view.addSubview(label0)
三、UIImageView
func makeImageView(){
let testImage:UIImage! = UIImage(named: "testOne")
let testImageView :UIImageView = UIImageView(frame: CGRectMake(20, 200, 100, 100))
testImageView.image = testImage
self.view .addSubview(testImageView)
// 可交互的
testImageView.userInteractionEnabled = true
// 制作简单动画跳转
let testImage1:UIImage! = UIImage(named: "test01")
let testImage2:UIImage! = UIImage(named: "test02")
let testImage3:UIImage! = UIImage(named: "QQNeedLogo")
// 设置图片,总共时间,跳转多少次
testImageView.animationImages = [testImage1,testImage2,testImage3]
testImageView.animationDuration = 3.0
testImageView.animationRepeatCount = 0 // 0 是无限次
// 开始动画
testImageView .startAnimating()
}
四、UIButton
func makeButton(){
let testButton:UIButton = UIButton(type: .System)
// 注意类型不一样
// public enum UIButtonType : Int {
case Custom // no button type
@available(iOS 7.0, *)
case System // standard system button
case DetailDisclosure
case InfoLight
case InfoDark
case ContactAdd
public static var RoundedRect: UIButtonType { get } // Deprecated, use UIButtonTypeSystem instead
}
testButton.frame = CGRect(origin: CGPoint(x: 20, y: 200), size: CGSize(width: 300, height: 40))
// 设置标题
testButton .setTitle("test", forState: UIControlState.Normal)
testButton.titleEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5)
/// 可以尝试 设置图片以及背景图片(type: .Custom)
// testButton .setImage(UIImage(named: "welcome_tap"), forState: UIControlState.Normal)
// 注意这个UIControl的方法使用
testButton.addTarget(self, action: Selector("testButtonAction:"), forControlEvents: .TouchUpInside)
self.view .addSubview(testButton)
}
func testButtonAction(button:UIButton){
print("tap")
print("\(button.currentTitle)")
}
五、UITextField
func makeUITextField()
{
let testTextField:UITextField = UITextField(frame:
CGRect(origin: CGPoint(x: 40, y: 100),
size: CGSize(width: 280, height: 40)))
// 代理,基本是必备的
testTextField.delegate = self
// 常用类型
/**
case None
case Line
case Bezel
case RoundedRect */
testTextField.borderStyle = .Line
testTextField.placeholder = "请输入密码"
testTextField.secureTextEntry = true // 是否密码键
testTextField.clearButtonMode = .Always // 清空的状态
self.view .addSubview(testTextField)
}
代理方法
func textFieldDidBeginEditing(textField: UITextField) {
}
func textFieldDidEndEditing(textField: UITextField) {
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField .resignFirstResponder()
return true
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
print("\(string)")
return true
}
六、UIScrollerView
同UITextField一样,都需要代理,这里先注意输入 Delegate 的区别
@interface LoginViewController ()<UIScrollViewDelegate>
class ViewController: UIViewController,UIScrollViewDelegate {}
接下了来还是其基本的属性和方法
func makeScrollerView(){
let learnScrollerView :UIScrollView = UIScrollView()
// 注意它的代理方法
learnScrollerView.delegate = self
learnScrollerView.frame = CGRect(origin: CGPoint(x: 0, y: 200), size: CGSize(width: 320, height: 300))
// 3倍的宽度
learnScrollerView.contentSize = CGSize(width: (320*3), height: 300)
learnScrollerView.bounces = true // 回弹
// 不显示的滑动条
learnScrollerView.showsHorizontalScrollIndicator = false
learnScrollerView.showsVerticalScrollIndicator = false
//刚好翻页
learnScrollerView.pagingEnabled = true
// 添加子视图
var index :Int
for(index = 0; index < 3;index++)
{
let subView :UIView = UIView()
subView.frame = CGRectMake(CGFloat(index * 320), 0, 320, 300)
// 获取随机颜色
let colorValue1 :CGFloat! = CGFloat(CGFloat(random())/CGFloat(RAND_MAX))
let colorValue2 :CGFloat! = CGFloat(CGFloat(random())/CGFloat(RAND_MAX))
let colorValue3 :CGFloat! = CGFloat(CGFloat(random())/CGFloat(RAND_MAX))
subView.backgroundColor = UIColor(red: colorValue1, green: colorValue2, blue: colorValue3, alpha: 1.0)
learnScrollerView.addSubview(subView)
}
self.view .addSubview(learnScrollerView)
}
当然它的代理
func scrollViewWillBeginDecelerating(scrollView: UIScrollView) {
}
func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
}
总结
整体来说,与OC相比,大部分的方法和属性都是一样的,只是用法稍微不同, 一开始不习惯,但用久了会觉得越来越方便,确实更整洁。当然上述只例举了部分的属性和方法,要了解更多,"command"点击进去,看它的接口方法就好了。