1c71 / Learn-iOS

学 iOS
0 stars 0 forks source link

iOS 学习历程(一个个截图而已) #2

Open 1c7 opened 9 years ago

1c7 commented 9 years ago

按钮

2015-10-21 8 27 20 2015-10-21 8 29 19 原本 左边的设计不是这样的,左边是一个 label,2个button,后面改了又撤销不回来。所以无所谓了。反正这帖子相当于某种学习日记而已。

1c7 commented 9 years ago

输入框

2015-10-21 9 14 32 只有2个功能,一个是点击按钮键盘会收起来,第二个是点击背景键盘也会收起来


import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var email: UITextField!

    @IBOutlet weak var password: UITextField!

    // 点击按钮收起键盘
    @IBAction func aaa(sender: UIButton) {

        self.email.resignFirstResponder()
        self.password.resignFirstResponder()

    }

    // 点击屏幕其他地方收起键盘
    // 输入 touchB 然后用回车自动补全就可以了
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

        self.view.endEditing(true)

    }

}
1c7 commented 9 years ago

滑动条

2015-10-22 1 41 56

1c7 commented 9 years ago

segment control

2015-10-22 2 28 41

1c7 commented 9 years ago

之前一直都是 Single View Application

现在开始多页的了

2015-10-22 2 33 15

1c7 commented 9 years ago

Tabbed Application

qq20151022-0

1c7 commented 9 years ago

qq20151022-3

1c7 commented 9 years ago

a6a23c48-c6b4-4d93-85b6-73b82ddc81b9

1c7 commented 9 years ago

列表 TableView

import UIKit

class ViewController: UIViewController, UITableViewDataSource {

    let people = [
        ("coffee", "james bruans"),
        ("water", "007"),
        ("orange juice", "kolu"),
        ("nice", "new york")
    ]

    let videos = [
        ("android development", "32 video"),
        ("Java", "0 video"),
        ("Python programeing", "10 video"),
        ("Web", "110 videos")
    ]

    // how many section
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 2
    }

    // how many row
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if section == 0{
            return people.count
        }else{
            return videos.count
        }

    }

    // content in each cell
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

         // indexPath 会从 0 开始,就是个循环

        var ceil = UITableViewCell()

        if indexPath.section == 0 {

            var (personName, personLocation) = people[indexPath.row]
            ceil.textLabel?.text = personName

        }else{

            var (videoTitle, videoDesc) = videos[indexPath.row]
            ceil.textLabel?.text = videoTitle

        }

        return ceil
    }

    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if section == 0{
            return "People"
        }else{
            return "Video"
        }

    }

    override func viewDidLoad() {
        super.viewDidLoad()

    }

}

2015-10-22 5 29 22

1c7 commented 9 years ago

画条线

import UIKit

class DrawExamples: UIView {

    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.

    override func drawRect(rect: CGRect) {
        // Drawing code
        // 画东西都写在这里面  并不是只能画长方形

        let context = UIGraphicsGetCurrentContext()
        CGContextSetLineWidth(context, 3.0)
        CGContextSetStrokeColorWithColor(context, UIColor.blueColor().CGColor)

        // create a path
        CGContextMoveToPoint(context, 30, 10)
        CGContextAddLineToPoint(context, 250, 350)

        //Actually draw the path
        CGContextStrokePath(context)
    }

}

2015-10-22 6 26 39

1c7 commented 9 years ago

画个长方形


import UIKit

class DrawExamples: UIView {

    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.

    override func drawRect(rect: CGRect) {
        // 画东西都写在这里面  并不是只能画长方形

        let context = UIGraphicsGetCurrentContext()
        CGContextSetLineWidth(context, 3.0)
        CGContextSetStrokeColorWithColor(context, UIColor.blueColor().CGColor)

        // create a path
//        CGContextMoveToPoint(context, 30, 10)
//        CGContextAddLineToPoint(context, 250, 350)

        /*
        CGContextMoveToPoint(context, 50, 50)
        CGContextAddLineToPoint(context, 100, 100)
        CGContextAddLineToPoint(context, 190, 230)
        CGContextAddLineToPoint(context, 60, 130)
        CGContextAddLineToPoint(context, 50, 50)
        */

        // 长方形
        let rect1 = CGRectMake(50, 50, 200, 400)
        CGContextAddRect(context, rect1)

        //Actually draw the path
        CGContextStrokePath(context)
    }

}
1c7 commented 9 years ago

重命名文件夹

要重命名文件夹 选中之后按下回车就好

删除文件

Command + Backspace 就是win键 加 删除前面文字的那个键

1c7 commented 9 years ago

画个图片上去

import UIKit

class DrawExamples: UIView {

    override func drawRect(rect: CGRect) {

        let context = UIGraphicsGetCurrentContext()
        let a = UIImage(named: "a.jpg")
        let b = UIImage(named: "b.jpg")

        let location = CGPointMake(25, 25)
        a?.drawAtPoint(location)

    }

}
1c7 commented 9 years ago

画个图片上去,填满屏幕

import UIKit

class DrawExamples: UIView {

    override func drawRect(rect: CGRect) {

        let context = UIGraphicsGetCurrentContext()
        let a = UIImage(named: "a.jpg")
        let b = UIImage(named: "b.jpg")

       /// let location = CGPointMake(25, 25)
        ///a?.drawAtPoint(location)

        let entireScreen = UIScreen.mainScreen().bounds
        a?.drawInRect(entireScreen)
        print(entireScreen)

    }

}

![Uploading 屏幕快照 2015-10-22 下午7.40.19.png…]()

1c7 commented 9 years ago

第一个简单小动画,按钮慢慢消失

页面上必须要有一个按钮叫 buckButton, 按 Ctrl 拖过去有个蓝线 取个名字就行了

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var buckButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        UIView.animateWithDuration(3.0, animations: {
            self.buckButton.alpha = 0
        })

    }

}
1c7 commented 9 years ago

放大并旋转动画

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var buckButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        UIView.animateWithDuration(3.0, animations: {

            let grow = CGAffineTransformMakeScale(4,4)
            // 变大四倍

            let angle = CGFloat(40)
            let rotate = CGAffineTransformMakeRotation(angle)
            // 旋转

            self.buckButton.transform = CGAffineTransformConcat(grow, rotate)
            // 两个动画一起执行

        })

    }

}

2015-10-22 7 59 36

1c7 commented 9 years ago

画2个方块到屏幕上


import UIKit

class ViewController: UIViewController {

    // Create two shape
    var greenSqure : UIView?
    var redSqure : UIView?

    override func viewDidLoad() {
        super.viewDidLoad()

        // 弄个绿色方块
        var dimen = CGRectMake(20, 20, 60, 60)
        greenSqure = UIView(frame: dimen)
        greenSqure?.backgroundColor = UIColor.greenColor()

        // 弄个红色方块
        dimen = CGRectMake(120, 120, 60, 60)
        redSqure = UIView(frame: dimen)
        redSqure?.backgroundColor = UIColor.redColor()

        // 加到屏幕上
        self.view.addSubview(greenSqure!)
        self.view.addSubview(redSqure!)

    }

}

2015-10-23 10 22 55

1c7 commented 9 years ago

方块 + 重力

会掉到屏幕外面去,因为没有底部


import UIKit

class ViewController: UIViewController {

    // Create two shape
    var greenSqure : UIView?
    var redSqure : UIView?
    var animator : UIDynamicAnimator?

    override func viewDidLoad() {
        super.viewDidLoad()

        // 弄个绿色方块
        var dimen = CGRectMake(20, 20, 60, 60)
        greenSqure = UIView(frame: dimen)
        greenSqure?.backgroundColor = UIColor.greenColor()

        // 弄个红色方块
        dimen = CGRectMake(120, 120, 60, 60)
        redSqure = UIView(frame: dimen)
        redSqure?.backgroundColor = UIColor.redColor()

        // 加到屏幕上
        self.view.addSubview(greenSqure!)
        self.view.addSubview(redSqure!)

        // Initialize the animator
        animator = UIDynamicAnimator(referenceView: self.view)

        // add gravity
        let gravity = UIGravityBehavior(items: [greenSqure!, redSqure!])
        let direction = CGVectorMake(0.0, 1.0)
        gravity.gravityDirection = direction

        animator?.addBehavior(gravity)

    }

}
1c7 commented 9 years ago

方块+重力+底部碰撞


import UIKit

class ViewController: UIViewController {

    // Create two shape
    var greenSqure : UIView?
    var redSqure : UIView?
    var animator : UIDynamicAnimator?

    override func viewDidLoad() {
        super.viewDidLoad()

        // 弄个绿色方块
        var dimen = CGRectMake(20, 20, 60, 60)
        greenSqure = UIView(frame: dimen)
        greenSqure?.backgroundColor = UIColor.greenColor()

        // 弄个红色方块
        dimen = CGRectMake(120, 120, 60, 60)
        redSqure = UIView(frame: dimen)
        redSqure?.backgroundColor = UIColor.redColor()

        // 加到屏幕上
        self.view.addSubview(greenSqure!)
        self.view.addSubview(redSqure!)

        // Initialize the animator
        animator = UIDynamicAnimator(referenceView: self.view)

        // add gravity
        let gravity = UIGravityBehavior(items: [greenSqure!, redSqure!])
        let direction = CGVectorMake(0.0, 1.0)
        gravity.gravityDirection = direction

        // 加上碰撞 效果
        let boundries = UICollisionBehavior(items: [greenSqure!, redSqure!])
        boundries.translatesReferenceBoundsIntoBoundary = true

        animator?.addBehavior(boundries)
        animator?.addBehavior(gravity)

    }

}
1c7 commented 9 years ago

![Uploading 屏幕快照 2015-10-23 上午10.41.42.png…]()

1c7 commented 9 years ago

方块+重力+更加给力的底部碰撞

import UIKit

class ViewController: UIViewController {

    // Create two shape
    var greenSqure : UIView?
    var redSqure : UIView?
    var animator : UIDynamicAnimator?

    override func viewDidLoad() {
        super.viewDidLoad()

        // 弄个绿色方块
        var dimen = CGRectMake(20, 20, 60, 60)
        greenSqure = UIView(frame: dimen)
        greenSqure?.backgroundColor = UIColor.greenColor()

        // 弄个红色方块
        dimen = CGRectMake(120, 120, 60, 60)
        redSqure = UIView(frame: dimen)
        redSqure?.backgroundColor = UIColor.redColor()

        // 加到屏幕上
        self.view.addSubview(greenSqure!)
        self.view.addSubview(redSqure!)

        // Initialize the animator
        animator = UIDynamicAnimator(referenceView: self.view)

        // add gravity
        let gravity = UIGravityBehavior(items: [greenSqure!, redSqure!])
        let direction = CGVectorMake(0.0, 1.0)
        gravity.gravityDirection = direction

        // 加上碰撞 效果
        let boundries = UICollisionBehavior(items: [greenSqure!, redSqure!])
        boundries.translatesReferenceBoundsIntoBoundary = true

        // Elasticity
        let bound = UIDynamicItemBehavior(items: [greenSqure!, redSqure!])
        bound.elasticity = 0.5

        animator?.addBehavior(boundries)
        animator?.addBehavior(gravity)
        animator?.addBehavior(bound)

    }

}

把步骤总结一下就是:

  1. create some shape
  2. add shape to the screen
  3. animtor object
  4. create some behavior
  5. animator.addBehavior