首页 > 代码库 > Swift开发之UIButton作为蒙板实现图片缩放

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

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

//

//  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];}}

}


技术分享

技术分享


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