gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:23:09 25 4
gpt4 key购买 nike

我从一个项目中获得了以下代码: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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com