ios - 为 iOS8 和 iOS7 兼容性转换 iOS9 合并触摸绘图代码

标签 ios swift optimization drawing

我从一个项目中获得了以下代码:https://github.com/FlexMonkey/SmoothScribble/tree/master/SmoothScribble

目前该项目仅在 iOS9 上运行,因为该项目使用仅适用于 iOS9 的 UIStackViewcoalescedTouchesForTouch。但是我希望转换该项目以使其也可用于 iOS7 和 iOS8。我该怎么做?

我想我可能知道如何用 UIView 替换 UIStackView。但是,如何处理引用 coalescedTouches 的部分?

例如,我如何转换下面的代码片段,使其符合适用于 iOS8 和 iOS9 的传统 touchesMoved

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?)
{
guard let
    touch = touches.first,
    coalescedTouches = event?.coalescedTouchesForTouch(touch),
    touchOrigin = touchOrigin
    else
{
    return
}

coalescedTouches.forEach
    {
        hermiteScribbleView.appendScribble($0.locationInView(touchOrigin))
        simpleScribbleView.appendScribble($0.locationInView(touchOrigin))
}
}

import UIKit

class ViewController: UIViewController
{
let stackView = UIStackView()

let hermiteScribbleView = HermiteScribbleView(title: "Hermite")
let simpleScribbleView = SimpleScribbleView(title: "Simple")

var touchOrigin: ScribbleView?

override func viewDidLoad()
{
    super.viewDidLoad()

    view.addSubview(stackView)

    stackView.addArrangedSubview(hermiteScribbleView)
    stackView.addArrangedSubview(simpleScribbleView)
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
{
    guard let
        location = touches.first?.locationInView(self.view) else
    {
        return
    }

    if(hermiteScribbleView.frame.contains(location))
    {
        touchOrigin = hermiteScribbleView
    }
    else if (simpleScribbleView.frame.contains(location))
    {
        touchOrigin = simpleScribbleView
    }
    else
    {
        touchOrigin = nil
        return
    }

    if let adjustedLocationInView = touches.first?.locationInView(touchOrigin)
    {
        hermiteScribbleView.beginScribble(adjustedLocationInView)
        simpleScribbleView.beginScribble(adjustedLocationInView)
    }
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?)
{
    guard let
        touch = touches.first,
        coalescedTouches = event?.coalescedTouchesForTouch(touch),
        touchOrigin = touchOrigin
        else
    {
        return
    }

    coalescedTouches.forEach
        {
            hermiteScribbleView.appendScribble($0.locationInView(touchOrigin))
            simpleScribbleView.appendScribble($0.locationInView(touchOrigin))
    }
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?)
{
    hermiteScribbleView.endScribble()
    simpleScribbleView.endScribble()
}

override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent?)
{
    if motion == UIEventSubtype.MotionShake
    {
        hermiteScribbleView.clearScribble()
        simpleScribbleView.clearScribble()
    }
}

override func viewDidLayoutSubviews()
{
    stackView.frame = CGRect(x: 0,
        y: topLayoutGuide.length,
        width: view.frame.width,
        height: view.frame.height - topLayoutGuide.length).insetBy(dx: 10, dy: 10)

    stackView.axis = view.frame.width > view.frame.height
        ? UILayoutConstraintAxis.Horizontal
        : UILayoutConstraintAxis.Vertical

    stackView.spacing = 10

    stackView.distribution = UIStackViewDistribution.FillEqually
}
}

完整的项目代码位于:项目位于:https://github.com/FlexMonkey/SmoothScribble/tree/master/SmoothScribble

最佳答案

昨天我也有一个关于兼容性的问题,结果发现可以通过可用性条件轻松解决,如下所示:

if #available(iOS 9.0, *) {
  let store = CNContactStore()
} else {
  // Fallback on earlier versions
}

您可以阅读这篇文章以了解更多信息。 http://useyourloaf.com/blog/checking-api-availability-with-swift.html

不知道大家有没有同样的问题。但是,在我看来,我可以有两段​​分别符合 iOS 9 和 iOS 8 的代码片段,并使用 Availability Condition@available Attribute 来解决兼容性问题。

关于ios - 为 iOS8 和 iOS7 兼容性转换 iOS9 合并触摸绘图代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34358306/

相关文章:

javascript - 我怎样才能让谷歌的闭包编译器消除属性

javascript - 异步旅行推销员子旅行的本地搜索启发式

ios - 注销后从内存中删除 View Controller ?

ios - 从 View 中移除和释放类型对象

ios - 使用remoteControlReceived控制音乐时遇到问题

ios - dequeueReusableCellWithIdentifier 中的 String 与 NSStringFromClass

ios - 如何设置应用程序以仅信任一个SSL证书

swift - 检查数组是否包含元素

ios - 将相同的框架与另一个框架和主应用程序一起使用时出现错误

optimization - 已知回路跳闸次数的优化可能性