• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

swift全屏浏览图片

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

我封装了一个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
        }
    }

}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap