【问题标题】:Instance member cannot be used不能使用实例成员
【发布时间】:2017-06-06 17:46:48
【问题描述】:

我有这个启动视图,但我在使用 use3dtouch 时遇到了问题,因为我收到一个错误消息,告诉我启动视图没有实例成员“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()
    }
}
 }

【问题讨论】:

  • 我该怎么做
  • 你能帮帮我吗
  • 我应该这样做“var SplashV = VPSplashView(use3dTouch)
  • 你把代码放在哪里了?在VPSplashView的类定义中?它是UIView 的子类吗?符合任何协议? use3DTouchlongPressed 是什么?你隐瞒太多了。请在询问更多信息之前显示更多代码。

标签: ios iphone swift swift3


【解决方案1】:

您添加的代码已经阐明了一些重要的事情来编写答案...

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

  • use3DTouch 是在VPSplashView 中明确声明的实例属性

  • longPressed(_:) 是在VPSplashView 中显式声明的实例方法

第三个在这种情况下并不重要,但前两个显然与您得到的错误消息有关:Instance member cannot be used

当您访问实例成员(在本例中为实例属性)时,您需要在 .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。最好删除它。

【讨论】:

    猜你喜欢
    • 2015-11-27
    • 1970-01-01
    • 2020-04-07
    • 2016-05-23
    • 2016-01-01
    • 2016-09-09
    • 2018-04-21
    相关资源
    最近更新 更多