SWIFT: Changing view controllers with CMDeviceMotion

不羁岁月 提交于 2019-12-19 11:43:24

问题


I am following this article: http://nshipster.com/cmdevicemotion/ On how to use CMDeviceMotion.

They named this part of the article: Keep It To Yourself

I made a viewcontroller with a identifier called: PromptViewController.

I get an error: Use of undeclared type 'PromptViewController' In the section:

 if let promptViewController = self?.storyboard?.instantiateViewControllerWithIdentifier("PromptViewController") as? PromptViewController {`

Here is my full code:

//  ViewController.swift
//  upload_images


import UIKit
import MapKit
import CoreLocation
import CoreMotion

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet var image: UIImageView!



override func viewDidLoad() {
    super.viewDidLoad()

    // get magnitude of vector via Pythagorean theorem
    func magnitudeFromAttitude(attitude: CMAttitude) -> Double {
        return sqrt(pow(attitude.roll, 2) + pow(attitude.yaw, 2) + pow(attitude.pitch, 2))
    }

    // initial configuration
    var initialAttitude = manager.deviceMotion!.attitude
    var showingPrompt = false

    // trigger values - a gap so there isn't a flicker zone
    let showPromptTrigger = 1.0
    let showAnswerTrigger = 0.8

    if manager.deviceMotionAvailable {
        manager.startDeviceMotionUpdatesToQueue(NSOperationQueue.mainQueue()) {
            [weak self] (data: CMDeviceMotion?, error: NSError?) in

            guard let data = data else { return }

            // translate the attitude
            data.attitude.multiplyByInverseOfAttitude(initialAttitude)

            // calculate magnitude of the change from our initial attitude
            let magnitude = magnitudeFromAttitude(data.attitude) ?? 0

            // show the prompt
            if !showingPrompt && magnitude > showPromptTrigger {
                if let promptViewController = self?.storyboard?.instantiateViewControllerWithIdentifier("PromptViewController") as? PromptViewController {
                    showingPrompt = true

                    promptViewController.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
                    self?.presentViewController(promptViewController, animated: true, completion: nil)
                }
            }

            // hide the prompt
            if showingPrompt && magnitude < showAnswerTrigger {
                showingPrompt = false
                self?.dismissViewControllerAnimated(true, completion: nil)
            }
        }
    }

}


@IBAction func selectPicture(sender: AnyObject) {

    let ImagePicker = UIImagePickerController()
    ImagePicker.delegate = self
    ImagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary

    self.presentViewController(ImagePicker, animated: true, completion: nil)


}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    image.image = info[UIImagePickerControllerOriginalImage] as? UIImage
    self.dismissViewControllerAnimated(true, completion: nil)
}



@IBAction func upload_request(sender: AnyObject) {
    UploadRequest()
}



func UploadRequest()
{
    let url = NSURL(string: "http://www.kaleidosblog.com/tutorial/upload.php")

    let request = NSMutableURLRequest(URL: url!)
    request.HTTPMethod = "POST"

    let boundary = generateBoundaryString()


    request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")

    if (image.image == nil)
    {
        return
    }

    let image_data = UIImagePNGRepresentation(image.image!)


    if(image_data == nil)
    {
        return
    }


    let body = NSMutableData()

    let fname = "test.png"
    let mimetype = "image/png"




    body.appendData("--\(boundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
    body.appendData("Content-Disposition:form-data; name=\"test\"\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
    body.appendData("hi\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)



    body.appendData("--\(boundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
    body.appendData("Content-Disposition:form-data; name=\"file\"; filename=\"\(fname)\"\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
    body.appendData("Content-Type: \(mimetype)\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
    body.appendData(image_data!)
    body.appendData("\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)


    body.appendData("--\(boundary)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)



    request.HTTPBody = body



    let session = NSURLSession.sharedSession()


    let task = session.dataTaskWithRequest(request) {
        (
        let data, let response, let error) in

        guard let _:NSData = data, let _:NSURLResponse = response  where error == nil else {
            print("error")
            return
        }

        let dataString = NSString(data: data!, encoding: NSUTF8StringEncoding)

        print(dataString)

    }

    task.resume()


}


func generateBoundaryString() -> String
{
    return "Boundary-\(NSUUID().UUIDString)"
}

Here is an image of my code error:

Image of my code

Do you know how I can fix this?

Thanks!

来源:https://stackoverflow.com/questions/38595784/swift-changing-view-controllers-with-cmdevicemotion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!