您的位置:首页 > 移动开发 > Swift

swift之imageview动画实例

2018-03-06 16:00 253 查看
import UIKit
class ViewController: UIViewController {    let h:CGFloat=UIScreen.main.bounds.size.height     let w:CGFloat=UIScreen.main.bounds.size.width    let ar:[String]=["dw","xw","h01","h02","h03","h04","h05","h06","h07","h08","h09","h10","h11","h12","h13","ho01","ho02","ho03","ho04","ho05","ho06","ho07","ho08","ho09","ho10","ho11","ho12","ho13","f01","f02","f03","f04","f05","f06","f07","f08","f09","f10","f11","f12","f13","m01","m02","m03","m04","m05","m06","m07","m08","m09","m10","m11","m12","m13"]    var imgAr=[UIImage]()    var tf:UITextField?//张数    var tf2:UITextField?//速度     var imagv:UIImageView=UIImageView.init(frame: CGRect.init(x: 50, y: 50, width: 150, height: 225))        override func viewDidLoad() {        super.viewDidLoad()                 setTF()           }    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {                view.endEditing(true)    }    //设置文本框    func setTF(){                tf=UITextField.init(frame: CGRect.init(x: 10, y: h-200, width: 100, height: 50))        tf?.keyboardType=UIKeyboardType.numberPad        tf?.textColor=UIColor.red        tf?.backgroundColor=UIColor.init(white: 0.8, alpha: 0.8)        tf?.placeholder="张数"        view.addSubview(tf!)        tf2=UITextField.init(frame: CGRect.init(x: 120, y: h-200, width: 100, height: 50))                tf2?.textColor=UIColor.red        tf2?.keyboardType=UIKeyboardType.numberPad         tf2?.backgroundColor=UIColor.init(white: 0.8, alpha: 0.8)        tf2?.placeholder="速度"        view.addSubview(tf2!)                //开始按钮        let rect:CGRect=CGRect.init(x: 10, y: h-100, width: 50, height: 50)        let font:UIFont=UIFont.systemFont(ofSize: 15)        let color:UIColor=UIColor.red        let bgcolor:UIColor=UIColor.white        let acs:Selector=#selector(ac)//事件        let btn:UIButton=setButton(rect: rect, title: "开始", htitle: nil, font: font, hfont: nil, titleColor: color, htitleColor: nil, imageName: nil, himageName: nil,bgcolor: bgcolor,action: acs,target:self)        view.addSubview(btn)                //结束按钮        let rect1:CGRect=CGRect.init(x: 80, y: h-100, width: 50, height: 50)        let font1:UIFont=UIFont.systemFont(ofSize: 15)        let color1:UIColor=UIColor.red        let bgcolor1:UIColor=UIColor.white        let acs1:Selector=#selector(stop)//事件        let btn1:UIButton=setButton(rect: rect1, title: "结束", htitle: nil, font: font1, hfont: nil, titleColor: color1, htitleColor: nil, imageName: nil, himageName: nil,bgcolor: bgcolor1,action: acs1,target:self)        view.addSubview(btn1)    }    //设置view    func setView(){        imagv.center=CGPoint.init(x: w/2, y: (h-200)/2)        imagv.image=UIImage.init(named: ar[1])// 默认的图片        for i in 0..<54{            let im:UIImage=UIImage.init(named: ar[i])!            imgAr.append(im)        }                view.addSubview(imagv)        imagv.animationImages=imgAr        //设置动画                let suStr=tf2?.text       //注意字符串不能一步转换成double,需要先转换成Int,在由Int转换成double        let i = Int(suStr!)        let d=Double.init(i!)        imagv.animationDuration = d//动画时长//        imagv.animationRepeatCount=Int((tf?.text)!)!//重复播放次数                //       let isA = imagv.isAnimating//是否正在动画                          }    //开始动画    @objc func ac(){        setView()         imagv.startAnimating()//开始动画
    }    //停止按钮    @objc func stop(){         imagv.stopAnimating()//停止动画    }       //创建按钮    func setButton(rect:CGRect,title:String?,htitle:String?,font:UIFont?,hfont:UIFont?,titleColor:UIColor?,htitleColor:UIColor?,imageName:String?,himageName:String?,bgcolor:UIColor?,action:Selector?,target:UIViewController?)->UIButton{        //参数依次是定位,标题,选中时的标题,标题字体大小,选中时的标题字体大小,标题颜色,选中时的标题颜色,图片,选中时的图片        let btn:UIButton=UIButton.init(frame: rect)        if htitle != nil && htitle != ""{//选中时的标题        btn.setTitle(htitle, for: UIControlState.highlighted)        }else if title != nil && title != ""{//            btn.setTitle(title, for: UIControlState.normal)        }        if hfont != nil{//选中时标题字体大小             btn.titleLabel?.font=hfont        }else if font != nil{//        btn.titleLabel?.font=font        }        if htitleColor != nil{//选中时标题颜色             btn.setTitleColor(htitleColor, for: UIControlState.highlighted)        }else if titleColor != nil{//        btn.setTitleColor(titleColor, for: UIControlState.normal)        }                if himageName != nil && himageName != ""{//选中时的图片            btn.setImage(UIImage.init(named: himageName!), for: UIControlState.highlighted)        }else if imageName != nil && imageName != ""{//             btn.setImage(UIImage.init(named: imageName!), for: UIControlState.normal)        }        if bgcolor != nil{//背景颜色            btn.backgroundColor=bgcolor        }        if action != nil{//添加事件            btn.addTarget(target, action: action!, for: UIControlEvents.touchUpInside)        }        return btn    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: