技术文章分类(180)

技术随笔(11)

swift全屏浏览图片

我封装了一个UIView类,只要触发事件调用即可

//浏览照片
    func browsePhoto(){
        let li_ImageViewer:Li_ImageViewer = Li_ImageViewer(frame:DeviceFrame)
        li_ImageViewer.imageView.image = UIImage(contentsOfFile:self.diary.photos)
        self.view.addSubview(li_ImageViewer)
        UIView.animateWithDuration(0.3, animations:{
            li_ImageViewer.backgroundColor = UIColor(white:0, alpha: 1)
            li_ImageViewer.imageView.alpha = 1
        })
        println("浏览照片")
    }

 

下面是类的代码

//
//  Li_ImageViewer.swift
//  SwiftStudy3
//
//  Created by dongway on 14-7-10.
//  Copyright (c) 2014年 dongway. All rights reserved.
//

import UIKit

class Li_ImageViewer: UIView {
    
    var imageView:UIImageView!
    var lastScale:CGFloat!

    init(frame: CGRect) {
        super.init(frame: frame)
        // Initialization code
        lastScale = 1.0
        
        self.backgroundColor = UIColor.blackColor()
        
        self.imageView = UIImageView()
        self.backgroundColor = UIColor.whiteColor()
        self.imageView.frame = CGRectMake(0,(DeviceFrame.height-DeviceFrame.width)/2,DeviceFrame.width,DeviceFrame.width)
        
        var gr1:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("handleTapGesture:"))
        var gr2:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("handleTapGesture:"))
        gr1.numberOfTapsRequired = 1
        gr1.requireGestureRecognizerToFail(gr2)//这里保证双击的时候不会出发单击时间
        gr2.numberOfTapsRequired = 2
        self.imageView.userInteractionEnabled = true
        self.imageView.addGestureRecognizer(gr1)
        self.imageView.addGestureRecognizer(gr2)
        
        var pin:UIPinchGestureRecognizer = UIPinchGestureRecognizer(target: self, action: Selector("handlePinchGesture:"))
        self.imageView.addGestureRecognizer(pin)
        
        
        self.addSubview(self.imageView)
    }
    
    /*
        单击self消失
        双击self.imageView放大和缩小
    */
    func handleTapGesture(sender:UITapGestureRecognizer){
        let touchCount:Int = sender.numberOfTapsRequired
        switch touchCount {
        case 1:
            dismiss()
        case 2:
            self.handleDoubleClick(sender)
        default:println("")
        }
    }
    
    /*
        手势:捏
    */
    func handlePinchGesture(sender:UIGestureRecognizer){
        var factor:CGFloat = (sender as UIPinchGestureRecognizer).scale
        sender.view.transform = CGAffineTransformMakeScale(factor
            , factor)
    }
    
    
    //self消失
    func dismiss(){
        
        UIView.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
            self.backgroundColor = UIColor(white:1, alpha: 0)
            self.imageView.alpha = 0
        }, completion: {(value:Bool) in
            self.removeFromSuperview()
        })
    }
    //双击放大和缩小
    func handleDoubleClick(sender:UIGestureRecognizer){
        //.ScaleAspectFit是原比例
        if sender.view.contentMode == .ScaleAspectFit {
            sender.view.contentMode = .Center
        }else{
            sender.view.contentMode = .ScaleAspectFit
        }
    }

}

 

posted @ 2014-07-10 16:52  坤哥MartinLi  阅读(1582)  评论(0编辑  收藏  举报