這里都是基于對View的屬性和方法進(jìn)行學(xué)習(xí)煎楣,注意與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鸠真,是默認(rèn)的偏移量
label0.shadowOffset = CGSizeMake(0, 1)
// 讓字體適應(yīng)寬度
// label0.adjustsFontSizeToFitWidth = true
// 可以多行
label0.numberOfLines = 0
// 當(dāng)字?jǐn)?shù)多的時候的處理方式悯仙,顯示那個 ... 以及位置龄毡,或者不顯示
// label0.lineBreakMode = NSLineBreakMode.ByClipping
// 顯示使用縮小文本位置基于最初的基線,后期可能會用的多
// label0.baselineAdjustment = UIBaselineAdjustment.AlignCenters
// iOS 9 新加的,默認(rèn)是False,設(shè)置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
// 制作簡單動畫跳轉(zhuǎn)
let testImage1:UIImage! = UIImage(named: "test01")
let testImage2:UIImage! = UIImage(named: "test02")
let testImage3:UIImage! = UIImage(named: "QQNeedLogo")
// 設(shè)置圖片锡垄,總共時間沦零,跳轉(zhuǎn)多少次
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))
// 設(shè)置標(biāo)題
testButton .setTitle("test", forState: UIControlState.Normal)
testButton.titleEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5)
/// 可以嘗試 設(shè)置圖片以及背景圖片(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 // 清空的狀態(tài)
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 的區(qū)別
@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)
// 獲取隨機(jī)顏色
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)
}
當(dāng)然它的代理
func scrollViewWillBeginDecelerating(scrollView: UIScrollView) {
}
func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
}
總結(jié)
整體來說,與OC相比搔谴,大部分的方法和屬性都是一樣的魁袜,只是用法稍微不同, 一開始不習(xí)慣己沛,但用久了會覺得越來越方便慌核,確實(shí)更整潔。當(dāng)然上述只例舉了部分的屬性和方法申尼,要了解更多垮卓,"command"點(diǎn)擊進(jìn)去,看它的接口方法就好了师幕。