#UI阶段 - UIImage和UIButton

UI阶段

UIImageView,UIButton

UIImageView 基础

 //UIImageView:UIView
        //1.创建UIIMageView对象
        let imageView = UIImageView.init(frame: CGRectMake(0, 0, 375, 667))
        //2.添加到界面上
        self.view.addSubview(imageView)
        imageView.backgroundColor = UIColor.redColor()
        
        
        //1.Image属性
        //如果图片的格式是png可以省略后缀,如果是其他格式就必须写出来
        imageView.image = UIImage.init(named: "33.gif")
        //通过图片路劲去创建一个图片对象
        //将文件放到工程中,实质是放在了当前应用程序的包文件中
        //想要拿到工程中的图片路径,先要获取包文件的路径
        let imagePath = NSBundle.mainBundle().pathForResource("33", ofType: "gif")//拿到包文件中指定文件的路径
        imageView.image = UIImage.init(contentsOfFile: imagePath!)
        //比较通过图片名和图片地址创建图片对象的两种方式
        //1.通过图片名创建的图片对象在程序结束后才会被销毁,只会创建一次;
        //2.通过图片地址创建的对象不再使用的时候就销毁
        //3.使用图片名创建图片的情况:创建小图标的时候,在工程中会重复使用的图片
        //4.使用图片地址创建图片的情况:创建大图片的时候,不会被频繁使用的图片
        
        
        //2.内容模式  字体的填充方式
        imageView.contentMode = .ScaleToFill

UIImage帧动画

timer的使用

d.设置动画的重复次数(默认是0 是无限循环)
imageview.animationRepeatCount = 5
//创建一个定时器,并自动开启
//参数1:定时时间
//参数2:调用方法的对象
//参数3:存储定时时间到了以后需要调用的方法(可以带参,只能带一个而且必须是NSTimer类型的参数)
//参数4:给当前的NSTimer的USERinfo属性赋值
//参数5:是否重复
NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "fly:", userInfo: "aaa", repeats: true

timer的暂停
//暂停计时器
            timer.fireDate = NSDate.distantFuture()
创建一个动画数组
//1.创建iamgeview
    //通过图片去创建一个imageview
    imageview = UIImageView.init(image:UIImage.init(named: "DOVE 1.png"))
    
    imageview.frame = CGRectMake(98, 313, 180, 180)
   
    //使用UIImageview播放帧动画
    //a.设置帧动画
    //创建一个空的图片数组
    var images = [UIImage]()
    for i in 1...13{
    let imageName = "\(i).png"
    //创建对应的图片
    let image = UIImage.init(named: imageName)
    images.append(image!)
    }
    imageview.animationImages = images
    //b.动画时间
    imageview.animationDuration = 0.5
    //C.开始动画
    imageview.startAnimating()
    self.view.addSubview(imageview)
        
        return imageview

UIButton的基础

创建文字按钮
//UIButton:UIControl:UIView
        //1.创建UIButton对象
        let titlebtn = UIButton.init(frame: CGRectMake(100, 500, 180, 180))
        self.view.addSubview(titlebtn)
        titlebtn.backgroundColor = UIColor.blueColor()
        
//        titlebtn.titleLabel
//        titlebtn.imageView
        //1。设置按钮上显示的文字
        //参数1:想要在按钮上显示的文字
        //参数2:状态
        
        
        //Disabled ->不可用的状态 (按钮不可被点击)
        titlebtn.setTitle("点击", forState: .Normal)
        titlebtn.setTitle("学生", forState: .Highlighted)
        //2.设置当前按钮是否被选中(默认非选中 false)
        titlebtn.selected = false
        titlebtn.setTitle("选中", forState: .Selected)
        //3.设置当前按钮不可用(默认是可用 true)
        titlebtn.enabled = true
        titlebtn.setTitle("不可用", forState: .Disabled)
        //4.设置文字颜色
        titlebtn.setTitleColor(UIColor.redColor(), forState: .Highlighted)
        //5.设置按钮上的文字字体
        //按钮上的文字和文字的颜色,必须通过对应的set方法去根据状态设置,其他的和文字相关的属性可以拿到titleLabel去设置
        titlebtn.titleLabel?.font = UIFont.systemFontOfSize(40)
        //6.设置按钮上的文字对齐方式
        titlebtn.titleLabel?.textAlignment = .Left
 titlebtn.setBackgroundImage(UIImage(named: "1111.png"), forState: .Normal)

给按钮添加事件

        //参数1:调用方法的对象
        //参数2:指定事件发生后参数1需要去调用的方法(这个方法可以带也可以不带,如果带参只能带一个,并且类型是UIButton。)
        //参数3:事件
        
        //TouchDown 按下的事件
        //TouchUpInside - >当按钮别按下弹起来的时候
        titlebtn.addTarget(self, action: "btnAction:", forControlEvents: .TouchDown)
        titlebtn.addTarget(self, action: "btnAction2:", forControlEvents: .TouchUpInside)
func  btnAction(btn:UIButton){
        btn.backgroundColor = UIColor(red: CGFloat(arc4random()%256)/255, green: CGFloat(arc4random()%256)/255, blue: CGFloat(arc4random()%256)/255, alpha: 1)
        btn.setImage(UIImage(named: "32.jpg"), forState: .Normal)
        
    }
    func btnAction2(btn:UIButton){
    
    btn.setImage(UIImage(named: "1111.jpg"), forState: .Normal)
    
    
    }
创建图片和文字按钮
let imageBtn = UIButton.init(frame: CGRectMake(100, 300, 180, 180))
        self.view.addSubview(imageBtn)
        imageBtn.setImage(UIImage(named: "32.jpg"), forState: .Normal)
        imageBtn.addTarget(self, action: "btnAction:", forControlEvents: .TouchDown)
        imageBtn.addTarget(self, action: "btnAction2:", forControlEvents: .TouchUpInside)
        imageBtn.setTitle("学生", forState: .Normal)
        imageBtn.titleLabel?.font = UIFont.systemFontOfSize(40)

自定义UIButton

let btn = YTButton()
定义一个YTButtn类
//图片高度是整个按钮高度的4/5
    //label标签的高度是整个按钮高度的1/5
    //重新计算Button上的imageView的坐标和大小
    //参数1:当前按钮的坐标和大小,只需要拿到按钮的大小范围
    //返回值:计算好了的图片的坐标和大小
    override func imageRectForContentRect(contentRect: CGRect) -> CGRect {
        
        let x:CGFloat = 0
        let y:CGFloat = 0
        let w:CGFloat = contentRect.size.width
        let h:CGFloat = contentRect.size.height*4/5
        
        return CGRectMake(x, y, w, h)
    }
    //重新计算Button上的titleLabel的坐标和大小
    //参数1:当前按钮的坐标和大小,只需要拿到按钮的大小范围
    //返回值:计算好了的图片的坐标和大小
    override func titleRectForContentRect(contentRect: CGRect) -> CGRect {
        let x:CGFloat = 0
        let y:CGFloat = contentRect.size.height*4/5
        let w:CGFloat = contentRect.size.width
        let h:CGFloat = contentRect.size.height*1/5
        return CGRectMake(x, y, w, h)
        
    }

杀戮地带 代码实战

![屏幕快照 2016-08-27 上午10.24.44.png](/Users/IOS1605/Desktop/屏幕快照 2016-08-27 上午10.24.44.png)

    var back = UIImageView()
    var person = UIImageView()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.createImageView()
        person.frame = CGRectMake(158, 313, 80, 80)
        back.addSubview(person)
        var images = [UIImage]()
        for i in 1...3{
            let imageName = "player_down_\(i).png"
            //创建对应的图片
            let image = UIImage.init(named: imageName)
            images.append(image!)
        }
        person.animationImages = images
        //b.动画时间
        person.animationDuration = 0.5
        //C.开始动画
        person.startAnimating()
        let up=UIButton.init(frame: CGRectMake(148, 452, 80, 80))
        up.setImage(UIImage(named: "button_up.png"), forState: .Normal)
        back.addSubview(up)
        up.addTarget(self, action: "upmove:", forControlEvents: .TouchUpInside)
        self.up()
        self.down()
        self.left()
        self.right()
    }
    func  btnAction(btn:UIButton){
        if person.frame.origin.y <= 610{
        person.frame.origin.y+=10
        var images = [UIImage]()
        for i in 1...3{
            let imageName = "player_down_\(i).png"
            //创建对应的图片
            let image = UIImage.init(named: imageName)
            images.append(image!)
        }
        person.animationImages = images
        //b.动画时间
        person.animationDuration = 0.1
        //C.开始动画
            person.startAnimating()
        }
        if person.frame.origin.y >= 620
        {
            timer2.fireDate = NSDate.distantFuture()
        }
    }
    func btnAction2(btn:UIButton){
        if person.frame.origin.y >= 0{
       person.frame.origin.y-=10
        var images = [UIImage]()
        for i in 1...3{
            let imageName = "player_up_\(i).png"
            //创建对应的图片
            let image = UIImage.init(named: imageName)
            images.append(image!)
        }
        person.animationImages = images
        //b.动画时间
        person.animationDuration = 0.1
        //C.开始动画
        person.startAnimating()
        }
        if person.frame.origin.y <= 20{
            timer1.fireDate = NSDate.distantFuture()
        }
    }
    func btnAction3(btn:UIButton){
        if person.frame.origin.x <= 300{
        person.frame.origin.x+=10
        var images = [UIImage]()
        for i in 1...3{
            let imageName = "player_right_\(i).png"
            //创建对应的图片
            let image = UIImage.init(named: imageName)
            images.append(image!)
        }
        person.animationImages = images
        //b.动画时间
        person.animationDuration = 0.1
        //C.开始动画
            person.startAnimating()}
        if person.frame.origin.x >= 320{
            
           timer3.fireDate = NSDate.distantFuture()
        }
    }
    func btnAction4(btn:UIButton){
        if person.frame.origin.x > -10{
        person.frame.origin.x-=10
        var images = [UIImage]()
        for i in 1...3{
            let imageName = "player_left_\(i).png"
            //创建对应的图片
            let image = UIImage.init(named: imageName)
            images.append(image!)
        }
        person.animationImages = images
        //b.动画时间
        person.animationDuration = 0.1
        //C.开始动画
            person.startAnimating()}
        if person.frame.origin.x <= -10{
            timer4.fireDate = NSDate.distantFuture()
        }
    }
     var timer1 = NSTimer()
     var timer2 = NSTimer()
     var timer3 = NSTimer()
     var timer4 = NSTimer()
    
    func upmove(){
    timer1 =  NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "btnAction2:", userInfo: "", repeats: true)
    }
    func downmove(){
       timer2 = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "btnAction:", userInfo: "", repeats: true)
    }
    func leftmove(){
      timer3 = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "btnAction4:", userInfo: "", repeats: true)
    }
    func rightmove(){
        timer4 =  NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "btnAction3:", userInfo: "", repeats: true)
    }
    func stop(){
        timer1.fireDate = NSDate.distantFuture()
        timer2.fireDate = NSDate.distantFuture()
        timer3.fireDate = NSDate.distantFuture()
        timer4.fireDate = NSDate.distantFuture()
        person.stopAnimating()
        person.image = UIImage.init(named:"player_up_1.png")
    }
    func stop2(){
        timer1.fireDate = NSDate.distantFuture()
        timer2.fireDate = NSDate.distantFuture()
        timer3.fireDate = NSDate.distantFuture()
        timer4.fireDate = NSDate.distantFuture()
        person.stopAnimating()
        person.image = UIImage.init(named:"player_down_1.png")
    }
    func stop3(){
        timer1.fireDate = NSDate.distantFuture()
        timer2.fireDate = NSDate.distantFuture()
        timer3.fireDate = NSDate.distantFuture()
        timer4.fireDate = NSDate.distantFuture()
        person.stopAnimating()
        person.image = UIImage.init(named:"player_left_1.png")
    }
    func stop4(){
        timer1.fireDate = NSDate.distantFuture()
        timer2.fireDate = NSDate.distantFuture()
        timer3.fireDate = NSDate.distantFuture()
        timer4.fireDate = NSDate.distantFuture()
        person.stopAnimating()
        person.image = UIImage.init(named:"player_right_1.png")
    }
    func up(){
        let up = UIButton.init(frame: CGRectMake(148, 452, 80, 80))
        self.view.addSubview(up)
        up.setImage(UIImage(named: "button_up.png"), forState: .Normal)
        up.addTarget(self, action: "stop", forControlEvents: .TouchUpInside)
        up.addTarget(self, action: "upmove", forControlEvents: .TouchDown)
    }
    func down(){
        let down = UIButton.init(frame: CGRectMake(148, 572, 80, 80))
        self.view.addSubview(down)
        down.setImage(UIImage(named: "button_down.png"), forState: .Normal)
        down.addTarget(self, action: "downmove", forControlEvents: .TouchDown)
        down.addTarget(self, action: "stop2", forControlEvents: .TouchUpInside)

    }
    func left(){
        let left = UIButton.init(frame: CGRectMake(88, 512, 80, 80))
        self.view.addSubview(left)
        left.setImage(UIImage(named: "button_left.png"), forState: .Normal)
        left.addTarget(self, action: "leftmove", forControlEvents: .TouchDown)
        left.addTarget(self, action: "stop3", forControlEvents: .TouchUpInside)

    }
    func right(){
        let right = UIButton.init(frame: CGRectMake(208, 512, 80, 80))
        self.view.addSubview(right)
        right.setImage(UIImage(named: "button_right.png"), forState: .Normal)
        right.addTarget(self, action: "rightmove", forControlEvents: .TouchDown)
        right.addTarget(self, action: "stop4", forControlEvents: .TouchUpInside)

    }
    func createImageView(){
        back = UIImageView.init(image:UIImage.init(named: "map.png"))
        back.frame = CGRectMake(0, 0, 375, 667)
        self.view.addSubview(back)
    }

你可能感兴趣的:(#UI阶段 - UIImage和UIButton)