Swift動態添加UIImageView並添加事件,

來源:互聯網
上載者:User

Swift動態添加UIImageView並添加事件,

Swift動態添加UIImageView並添加事件:

1. 建立UIImageView執行個體,並進行初始化

2. 設定UIImageView的使用者互動屬性userInteractionEnabled為True

3. 添加手勢識別事件

4. 實現事件

//擷取螢幕的尺寸        print(UIScreen.mainScreen().bounds)        //擷取螢幕應用程式尺寸(不包括狀態列)        print(UIScreen.mainScreen().applicationFrame)                //定義UIImageView集        var data:[(String,[Int])]=[            ("0.png",[30,80,100,150]),            ("half.png",[160,80,100,150]),            ("1.png",[290,80,100,150]),            ("2.png",[30,260,100,150]),            ("3.png",[160,260,100,150]),            ("5.png",[290,260,100,150]),            ("8.png",[30,440,100,150]),            ("13.png",[160,440,100,150]),            ("infinitegreatness.png",[290,440,100,150])        ]        
    //動態添加UIImageView for i in 0..<data.count{ let (name,rect) = data[i] let image:UIImageView = UIImageView.init(frame: CGRect(x: rect[0], y: rect[1], width: rect[2], height: rect[3])) image.image = UIImage(named: name)
        //Enable使用者互動 image.userInteractionEnabled = true
        //添加手勢識別 image.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "touchSelect:")) self.view.addSubview(image) }
func touchSelect(sender: UITapGestureRecognizer)->Void{                let newImage:UIImageView = UIImageView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height))        newImage.image = UIImage(named: "1.png")        self.view.addSubview(newImage)    }

  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.