GVKun编程网logo

ios – 在Swift 2中加载STL对象(金属)(swift struct class)

1

此处将为大家介绍关于ios–在Swift2中加载STL对象(金属)的详细内容,并且为您解答有关swiftstructclass的相关问题,此外,我们还将为您介绍关于ios–NSDate()VSNSDa

此处将为大家介绍关于ios – 在Swift 2中加载STL对象(金属)的详细内容,并且为您解答有关swift struct class的相关问题,此外,我们还将为您介绍关于ios – NSDate()VS NSDate.date()在Swift、ios – 在Swift 1.2中将自身作为参数传递给init方法、ios – 在Swift 2.0中以横向模式使用UIImagePickerController、ios – 在swift 2.1中使用MapKit添加不同的引脚颜色的有用信息。

本文目录一览:

ios – 在Swift 2中加载STL对象(金属)(swift struct class)

ios – 在Swift 2中加载STL对象(金属)(swift struct class)

我正在尝试创建一个3D模型查看器,我已经完成了一些OpenGL和Metal的教程来创建3D模型并使用平移手势等进行一些基本旋转.这个例子来自 www.raywenderlich.com,一个 Sample Project Git(error free),一个关于web(原始代码)与Xcode 7.1.1有一些错误.我想加载STL文件以显示在设备而不是多维数据集上.我搜索了很多,但找不到加载STL文件的教程,将3d对象投影到视图(Metal / Swift)上.

任何帮助都会很棒.我正在使用swift 2和xCode 7.1.1

解决方法

在iOS 9(和OS X 10.11和tvOS)中,Model I/O框架提供了从文件格式(包括)STL加载3D资源的帮助.通过将模型I / O与 GLKit或 MetalKit一起使用,您可以将这些资产直接加载到OpenGL或Metal缓冲区中进行渲染.

Apple有一个名为MetalKitEssentials的示例代码项目,其中显示了一些正在运行的内容 – 它是ObjC,但所有相关的API调用在Swift中都是相同的,所以它很容易翻译,特别是如果你已经有一些工作的Swift Metal代码参考.

ios – NSDate()VS NSDate.date()在Swift

ios – NSDate()VS NSDate.date()在Swift

@H_301_1@
我跟着 Bloc.io Swiftris tutorial,他们初始化一个日期:

lastTick = NSDate.date()

这会导致编译错误:

'date()' is unavailable: use object construction 'NSDate()'

应该等于:

NSDate *lastTick = [NSDate date];

(从NSDate reference)

Apple改变了Swift interface to NSDate,因为我看到使用NSDate.date的其他例子?

这只是NSDate或者你不能为任何Objective-C API调用类型方法?

解决方法

[NSDate date]是构造NSDate对象的工厂方法。

如果你阅读了“使用Swift与Cocoa和Objective-C”指南,有一个与Objective-C apis交互的部分:

For consistency and simplicity,Objective-C factory methods get mapped as convenience initializers in Swift. This mapping allows them to be used with the same concise,clear Syntax as initializers.”

Excerpt From: Apple Inc. “Using Swift with Cocoa and Objective-C.” iBooks. 07000

所以工厂方法:

[NSDate date]

在Swift中转换为初始化程序

NSDate()

它不只是NSDate你会发现这种模式,但在其他Cocoa API的工厂方法。

ios – 在Swift 1.2中将自身作为参数传递给init方法

ios – 在Swift 1.2中将自身作为参数传递给init方法

以下类将’let’属性声明为隐式展开的变量.这之前适用于 Xcode 6.2:
class SubView: UIView {

    let pandGestureRecognizer: UIPanGestureRecognizer!

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.pandGestureRecognizer = UIPanGestureRecognizer(target: self,action: "panAction:")
    }

    func panAction(gesture: UIPanGestureRecognizer) {
        // ...
    }
}

更新到Xcode 6.3(使用Swift 1.2)后,会发生以下编译错误:

Property 'self.panGestureRecognizer' not initialized at super.init call
Immutable value 'self.panGestureRecognizer' may only be initialized once

在super.init调用之前移动以下行:

self.pandGestureRecognizer = UIPanGestureRecognizer(target: self,action: "panAction:")

给出以下错误:

'self' is used before super.init call

属性’panGestureRecognizer’不需要变异,因此必须将其声明为常量’let’.由于它是常量,因此必须在声明时具有初始值,或者在init方法中初始化它.要初始化它,需要在’target’参数中传递’self’.

其他线程建议将其声明为隐式解包可选,并在super.init调用后初始化它.这之前一直有效,直到我更新到Xcode 6.3.

有没有人知道这种情况的正确实施或解决方法?

解决方法

问题

问题是你使用let-optionals声明为let没有给出默认值nil(然而var是).以下是在Swift 1.2中引入的,否则将无效,因为在声明之后你将无法给myOptional一个值:

let myOptional: Int?

if myCondition {
    myOptional = 1
} else {
    myOptional = nil
}

因此,你得到错误’属性’self.panGestureRecognizer’未在super.init调用’初始化’因为在调用super.init(coder:aDecoder)之前,因为panGestureRecognizer不是nil;它根本没有初始化.

解决方案:

1.将panGestureRecognizer声明为var,意味着它将被赋予默认值nil,然后在调用super.init(coder:aDecoder)之后可以更改.

2.在我看来,更好的解决方案是:不要使用隐式展开的可选项,并使用初始值UIPanGestureRecognizer()声明panGestureRecognizer.然后在调用super.init后设置目标:

class SubView: UIView {
    let panGestureRecognizer = UIPanGestureRecognizer()

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        panGestureRecognizer.addTarget(self,action: Selector("panAction:"))
    }
}

ios – 在Swift 2.0中以横向模式使用UIImagePickerController

ios – 在Swift 2.0中以横向模式使用UIImagePickerController

我正在编写仅限LandScape的iPad应用程序,我需要从库中拍照才能发送数据库,但图像上传屏幕仅适用于纵向模式.如何将其更改为横向模式?我读过一些关于UIPickerControllerDelegate的东西不支持横向模式,但是一些应用程序(例如iMessage)已经在使用它了.

这是我的代码:

class signUpViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate {


func imagePickerController(picker: UIImagePickerController,didFinishPickingImage image: UIImage!,editingInfo: [NSObject : AnyObject]!) {

    print("Image Selected")

    self.dismissViewControllerAnimated(true,completion: nil)

    profileImageView.image = image

}



@IBAction func importimage(sender: AnyObject) {

    var image = UIImagePickerController()
    image.delegate = self
    image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    image.allowsEditing = false

    self.presentViewController(image,animated: true,completion: nil)
}
}

解决方法

它绝对支持横向模式.把这个扩展放在某个地方.最好在名为UIImagePickerController的文件SupportedOrientations.swift中

extension UIImagePickerController
{
    public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return .Landscape
    }
}

这使得所有UIImagePickerControllers都在您的应用程序环境中.您也可以对其进行子类化并覆盖此方法,以仅使子类具有横向能力:

class LandscapePickerController: UIImagePickerController
{
    public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return .Landscape
    }
}

最后,为了支持所有方向,您可以返回

return [.Landscape,.Portrait]

对于Swift 3:

extension UIImagePickerController
    {
        override open var shouldAutorotate: Bool {
                return true
        }
        override open var supportedInterfaceOrientations : UIInterfaceOrientationMask {
                return .all
        }
}

ios – 在swift 2.1中使用MapKit添加不同的引脚颜色

ios – 在swift 2.1中使用MapKit添加不同的引脚颜色

我是 Swift的新手.我正在尝试在特定引脚上使用不同颜色的引脚或自定义引脚.我的代码有效.我有一个紫色的针,但我希望它们之间有所区别.我该怎么做?我认为在MapView委托方法中有一些事情要做,但我没有找到它.
import UIKit
import MapKit

class MapsViewController: UIViewController,MKMapViewDelegate{
    var shops: NSArray? {
        didSet{
            self.loadMaps()
        }
    }

    @IBOutlet weak var map: MKMapView?

    override func viewDidLoad() {
        super.viewDidLoad()
        loadMaps()
        self.title = "Carte"
        self.map!.delegate = self

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // dispose of any resources that can be recreated.
    }
    func mapView(mapView: MKMapView,viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        // simple and inefficient example

        let annotationView = MKPinAnnotationView()

        annotationView.pinTintColor = UIColor.purpleColor()
        return annotationView
    }

    func loadMaps(){
//        navigationController?.navigationBar.topItem!.title = "Carte"
        let shopsArray = self.shops! as NSArray
        for shop in shopsArray  {

            let location = CLLocationCoordinate2D(
                latitude: shop["lat"] as! Double,longitude: shop["long"] as! Double
            )


            let annotation = MKPointAnnotation()
            annotation.coordinate   = location
            annotation.title        = shop["name"] as? String
            annotation.subtitle     = shop["addresse"] as? String

            map?.addAnnotation(annotation)

        }





        // add point



    }

    /*
    // 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.
    }
    */

}

解决方法

更好的方法是使用实​​现MKAnnotation协议的自定义注释类(一种简单的方法是子类化MKPointAnnotation)并添加帮助实现自定义逻辑所需的任何属性.

在自定义类中,添加一个属性,比如pinColor,您可以使用该属性来自定义注释的颜色.

此示例是MKPointAnnotation的子类:

import UIKit
import MapKit

class ColorPointAnnotation: MKPointAnnotation {
    var pinColor: UIColor

    init(pinColor: UIColor) {
        self.pinColor = pinColor
        super.init()
    }
}

创建ColorPointAnnotation类型的注释并设置其pinColor:

let annotation = ColorPointAnnotation(pinColor: UIColor.blueColor())
annotation.coordinate = coordinate
annotation.title = "title"
annotation.subtitle = "subtitle"
self.mapView.addAnnotation(annotation)

在viewForAnnotation中,使用pinColor属性设置视图的pinTintColor:

func mapView(mapView: MKMapView,viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation {
        return nil
    }

    let reuseId = "pin"
    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
    if pinView == nil {
        pinView = MKPinAnnotationView(annotation: annotation,reuseIdentifier: reuseId)

        let colorPointAnnotation = annotation as! ColorPointAnnotation
        pinView?.pinTintColor = colorPointAnnotation.pinColor
    }
    else {
        pinView?.annotation = annotation
    }

    return pinView
}

今天关于ios – 在Swift 2中加载STL对象(金属)swift struct class的讲解已经结束,谢谢您的阅读,如果想了解更多关于ios – NSDate()VS NSDate.date()在Swift、ios – 在Swift 1.2中将自身作为参数传递给init方法、ios – 在Swift 2.0中以横向模式使用UIImagePickerController、ios – 在swift 2.1中使用MapKit添加不同的引脚颜色的相关知识,请在本站搜索。

本文标签: