30天学习Swift项目实战第四天------滑动菜单(UIScrollView)

前端之家收集整理的这篇文章主要介绍了30天学习Swift项目实战第四天------滑动菜单(UIScrollView)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

注意:项目分为3个界面,中间的界面使用了摄像头,模拟器是不能完成的。 源码分为4个文件ViewController.swift: // // ViewController.swift // SlideMenu // // Created by luopan on 16/8/5. // Copyright © 2016年 Hust University. All rights reserved. //

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var scrollView: UIScrollView!

override func viewDidLoad() {
    super.viewDidLoad()
    
    UIApplication.sharedApplication().statusBarHidden = true
    
    let leftView: LeftView = LeftView(nibName: "LeftView",bundle: nil)
    let centerView: CameraView = CameraView(nibName: "CameraView",bundle: nil)
    let rightView: RightView = RightView(nibName: "RightView",bundle: nil)
    
    
    self.addChildViewController(leftView)
    self.scrollView.addSubview(leftView.view)
    leftView.didMoveToParentViewController(self)
    
    self.addChildViewController(rightView)
    self.scrollView.addSubview(rightView.view)
    rightView.didMoveToParentViewController(self)
    
    self.addChildViewController(centerView)
    self.scrollView.addSubview(centerView.view)
    centerView.didMoveToParentViewController(self)
    
    var centerViewFrame: CGRect = centerView.view.frame
    centerViewFrame.origin.x = self.view.frame.width
    centerView.view.frame = centerViewFrame
    
    var rightViewFrame: CGRect = rightView.view.frame
    rightViewFrame.origin.x = 2 * self.view.frame.width
    rightView.view.frame = rightViewFrame
    
    
    self.scrollView.contentSize = CGSizeMake(self.view.frame.width * 3,self.view.frame.size.height)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

LeftView: // // LeftView.swift // SlideMenu // // Created by luopan on 16/8/5. // Copyright © 2016年 Hust University. All rights reserved. //

import UIKit

class LeftView: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    //Status Bar 给隐藏了
    UIApplication.sharedApplication().statusBarHidden = true
    
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application,you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

RightView: // // RightView.swift // SlideMenu // // Created by luopan on 16/8/5. // Copyright © 2016年 Hust University. All rights reserved. //

import UIKit

class RightView: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    //Status Bar 给隐藏了
    UIApplication.sharedApplication().statusBarHidden = true

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application,sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

CameraView: // // CameraView.swift // SlideMenu // // Created by luopan on 16/8/5. // Copyright © 2016年 Hust University. All rights reserved. //

import UIKit //导入照相机框架 import AVFoundation

class CameraView: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate{

@IBOutlet weak var cameraView: UIView!
@IBOutlet weak var tempImageView: UIImageView!

//有关照相机的变量
var captureSession : AVCaptureSession?
var stillImageOutput : AVCaptureStillImageOutput?
var previewLayer : AVCaptureVideoPreviewLayer?

override func viewDidLoad() {
    super.viewDidLoad()
    //去掉状态栏
    UIApplication.sharedApplication().statusBarHidden = true
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    
    previewLayer?.frame = cameraView.bounds
}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    
    captureSession = AVCaptureSession()
    captureSession?.sessionPreset = AVCaptureSessionPreset1280x720
    
    let backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
    var error : NSError?
    var input: AVCaptureDeviceInput!
    
    do {
        input = try AVCaptureDeviceInput(device: backCamera) }
    catch let error1 as NSError {
        error = error1
        input = nil
    }
    
    if (error == nil && captureSession?.canAddInput(input) != nil) {
        
        captureSession?.addInput(input)
        
        stillImageOutput = AVCaptureStillImageOutput()
        stillImageOutput?.outputSettings = [AVVideoCodecKey : AVVideoCodecJPEG]
        
        if (captureSession?.canAddOutput(stillImageOutput) != nil) {
            captureSession?.addOutput(stillImageOutput)
            
            previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
            previewLayer?.videoGravity = AVLayerVideoGravityResizeAspect
            previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.Portrait
            cameraView.layer.addSublayer(previewLayer!)
            captureSession?.startRunning()
        }
        
    }
    
}

func didPressTakePhoto(){
    
    if let videoConnection = stillImageOutput?.connectionWithMediaType(AVMediaTypeVideo){
        videoConnection.videoOrientation = AVCaptureVideoOrientation.Portrait
        stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection,completionHandler: {
            (sampleBuffer,error) in
            
            if sampleBuffer != nil {
                
                
                let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                let dataProvider  = CGDataProviderCreateWithCFData(imageData)
                let cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider,nil,true,CGColorRenderingIntent.RenderingIntentDefault)
                
                let image = UIImage(CGImage: cgImageRef!,scale: 1.0,orientation: UIImageOrientation.Right)
                
                self.tempImageView.image = image
                self.tempImageView.hidden = false
                
            }
            
            
        })
    }
    
    
}


var didTakePhoto = Bool()

func didPressTakeAnother(){
    if didTakePhoto == true{
        tempImageView.hidden = true
        didTakePhoto = false
        
    }
    else{
        captureSession?.startRunning()
        didTakePhoto = true
        didPressTakePhoto()
        
    }
    
}

override func touchesBegan(touches: Set<UITouch>,withEvent event: UIEvent?) {
    didPressTakeAnother()
}

}

最后,我们有四个界面设计的文件。UIImageView是没有用户交互功能是的,但是可以用一个UIView来覆盖并设置用户交互属性就可以达到效果

注意:所有的源码都可以找QQ是709165253的人要。 索要源码的时候,最好说明是在开源中国看到的。

原文链接:https://www.f2er.com/swift/323242.html

猜你在找的Swift相关文章