实例成员不能使用

时间:2017-01-21 23:23:51

标签: ios iphone swift swift3

我有这个启动视图,而且我遇到了use3dtouch的问题,因为我告诉我splashview没有实例成员' use3dtouch'。这是代码。

这是错误的图像

The error

import Foundation
import UIKit

class VPSplashView : UIView {
var vp = VPSplashView()
private lazy var __once: () = {
        if VPSplashView.traitCollection.forceTouchCapability ==     UIForceTouchCapability.unavailable
        {
            let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(VPSplashView.longPressed(_:)))
            self.addGestureRecognizer(longPressRecognizer)
            VPSplashView.use3DTouch = false
        } else {
            VPSplashView.use3DTouch = true
        }

    }()

static func addSplashTo(_ view : UIView, menuDelegate: MenuDelegate) -> VPSplashView{
    let splashView = VPSplashView(view: view)
    splashView.backgroundColor = UIColor.clear
    splashView.isExclusiveTouch = true

    if (view.isKind(of: UIScrollView.classForCoder())){
        (view as! UIScrollView).canCancelContentTouches = false
    }

    splashView.menu?.delegate = menuDelegate
    return splashView
}

// MARK: Initialization
var menu : VPSplashMenu?

fileprivate var use3DTouch : Bool = true

var onceToken: Int = 0

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

init(view: UIView){
    super.init(frame:view.bounds)
    view.addSubview(self)
    self.menu = VPSplashMenu.init(center: self.center)
}

func setDataSource(_ source: MenuDataSource!){
    self.menu?.dataSource = source
}

override func layoutSubviews() {
     super.layoutSubviews()
    self.superview?.bringSubview(toFront: self)

    if (self.superview != nil){
        self.setup()
    }
}

fileprivate func setup(){
    _ = self.__once;
}

// MARK: Long Press Handling
func longPressed(_ sender: UILongPressGestureRecognizer)
{
    switch sender.state {
    case .began:
        let centerPoint  = sender.location(in: self)
        menu?.movedTo(centerPoint)
        menu?.showAt(self)
        menu?.squash()

    case .ended:
        menu?.cancelTap()
        menu?.removeFromSuperview()
    case .changed:
        let centerPoint = sender.location(in: self)
        menu?.handleTap((menu?.convert(centerPoint, from: self))!)
    default:
        menu?.removeFromSuperview()
    }
}

// MARK: Touch Handling
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if (use3DTouch == true){
        var centerPoint : CGPoint = CGPoint.zero
        for touch in touches {
            centerPoint = touch.location(in: self)
            menu?.movedTo(centerPoint)
            menu?.showAt(self)
        }
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    if (use3DTouch == true){
        for touch in touches {
            let centerPoint = touch.location(in: self)
            if (menu?.shown == false){
                menu?.movedTo(centerPoint)
                if (touch.force > minimalForceToSquash){
                    menu?.squash()
                }
            } else {
                menu?.handleTap((menu?.convert(centerPoint, from: self))!)
            }
        }
    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if (use3DTouch == true){
        menu?.hide()
    }
}

override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
    if (use3DTouch){
        menu?.hide()
    }
}
 }

1 个答案:

答案 0 :(得分:0)

您添加的代码澄清了一些重要的事情来写答案......

  • traitCollection是一个在UITraitEnvironment中声明的实例属性,其中UIView符合

  • use3DTouch是一个实例属性,在VPSplashView明确声明

  • longPressed(_:)是一个明确在VPSplashView中声明的实例方法

在这种情况下,第三个并不重要,但前两个与您收到的错误消息明显相关:无法使用实例成员

当您访问实例成员(在本例中为实例属性)时,您需要在.memberName之前为实例引用添加前缀,而不是类名。

在您的情况下,self似乎是一个合适的实例:

private lazy var __once: () = {
    //###
    if self.traitCollection.forceTouchCapability ==     UIForceTouchCapability.unavailable
    {
        let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(self.longPressed(_:)))
        self.addGestureRecognizer(longPressRecognizer)
        //###
        self.use3DTouch = false
    } else {
        //###
        self.use3DTouch = true
    }
}()

但是你的处理方式比需要的要复杂得多,所以你可能还需要一些修复。

这一行至关重要:

var vp = VPSplashView()

这可能会导致一些运行时问题(即使您已解决了一些编译时问题)并且未使用vp。最好将其删除。