程序员人生 网站导航

Swift开发之UIButton作为蒙板实现图片缩放

栏目:综合技术时间:2015-03-03 07:51:36

很多火伴们们总是认为缩放图片的时候很是麻烦,由于不知道从哪里下手,特别是面对新的开发语言,其实图片缩放有很多种方法,但是万变不离其宗,那就是frame,所以,不要想的那末高大上。下面是1个利用蒙板实现的图片缩放demo,感兴趣的火伴们可以看1下。

//

//  ViewController.swift

//  图片的缩放

//

//  Created by 悦兑科技 on 15/1/15.

//  Copyright (c) 2015 BSY. All rights reserved.

//


import UIKit


class ViewController: UIViewController {


    var imageview = UIImageView()

    var button = UIButton()

    

    override func viewDidLoad() {

        super.viewDidLoad()

        

        // 初始化UIImageView

        var imageview:UIImageView = UIImageView(frame: CGRectMake(100, 100, 100, 100))

        var image = UIImage(named: "1024")

        imageview.image = image

        self.view.addSubview(imageview)

        self.imageview = imageview

        // 初始化UIButton(蒙板)

        var button:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton

        var frame = imageview.frame

        button.frame = frame

        self.view.addSubview(button)

        button.addTarget(self, action: "changeImage:", forControlEvents: UIControlEvents.TouchUpInside)

        self.button = button

        self.button.selected = false

    }

    // 初始化UIButton方法实现

    func changeImage(sender:UIButton){

        [UIView .beginAnimations("", context: nil)];

        [UIView .setAnimationDelay(0.1)];

        [UIView .setAnimationDuration(0.5)];

        self.button.selected = !sender.selected

        if(self.button.selected == true){

        self.imageview.frame = UIScreen.mainScreen().bounds

        }else if(self.button.selected == false){

        self.imageview.frame = CGRectMake(100, 100, 100, 100)

        [UIView.commitAnimations];}}

}





------分隔线----------------------------
------分隔线----------------------------

最新技术推荐