Swift NSAnimationContext.runAnimationGroup问题
我正在设置一个广告自动售票机,在我编写的应用程序的第二个显示屏上显示的窗口中运行广告图像 我终于能够让NSAnimationContext.runAnimationGroup做我需要它做的事情。然而,现在我有几个问题。首先,当图像滚动(转换)到新原点时,会留下潜线。其次,图像在转换时似乎会加速,然后变慢。速度似乎与持续时间直接相关,这是可以理解的,但是它开始缓慢,然后在中间加速,然后当图像到达窗口右侧时减慢很多。 我的故事板视图设置为2200x1100Swift NSAnimationContext.runAnimationGroup问题,swift,Swift,我正在设置一个广告自动售票机,在我编写的应用程序的第二个显示屏上显示的窗口中运行广告图像 我终于能够让NSAnimationContext.runAnimationGroup做我需要它做的事情。然而,现在我有几个问题。首先,当图像滚动(转换)到新原点时,会留下潜线。其次,图像在转换时似乎会加速,然后变慢。速度似乎与持续时间直接相关,这是可以理解的,但是它开始缓慢,然后在中间加速,然后当图像到达窗口右侧时减慢很多。 我的故事板视图设置为2200x1100 // // ViewController
//
// ViewController.swift
// Image Ticker
//
// Created by Thomas Carroll on 5/11/21.
//
import Cocoa
class ViewController: NSViewController {
var adFilesArray:Array<String> = []
var adImageView:Array<NSImageView> = []
var imageView:NSImageView!
var currentAd:Int = -1
var adFinished = false {
didSet {
if adFinished {
// Increment which ad to display
currentAd += 1
// If the current ad is the last ad in the array
if currentAd == adFilesArray.count {
// Reset to the first ad in the array
currentAd = 0
}
// Reset the observed property to true
adFinished = true
// Retrigger the showNextAd method
showNextAd()
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// Load ad file names array and the image views
do {
adFilesArray = try FileManager.default.contentsOfDirectory(atPath: "/Users/Shared/Ad Images")
for i in 0...adFilesArray.count-1 {
imageView = NSImageView(image: NSImage(byReferencingFile: "/Users/Shared/Ad Images/"+adFilesArray[i])!)
adImageView.append(imageView)
}
} catch {
print("Unable to retrieve images for ads.")
exit(1)
}
// Set the adFinished observed property to true to trigger the ads to begin
adFinished = true
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
func showNextAd() {
// Load the imageView
imageView = adImageView[currentAd]
// Do not set auto resize constraints
imageView.translatesAutoresizingMaskIntoConstraints = false
// Set the initial location just off the left side of the screen
imageView.frame = CGRect(x: -imageView.image!.size.width, y: 200, width: 1100, height: 700)
// Add the image view as a subview
self.view.addSubview(imageView)
// Begin the animation
NSAnimationContext.runAnimationGroup({ context in
//What is being animated? I want an imageView to scroll from the left (off screen) to the right (off screen)
//Indicate the duration of the animation
context.duration = 15.0
// Set the x coordinate of the origin (destination) to the screen width)
let origin = CGPoint(x: 2201, y: 200)
// Set the origin to where the image will animate (scroll) to
self.view.subviews[0].animator().frame.origin = origin
}, completionHandler: {
// Remove the subview
self.view.subviews.remove(at: 0)
// Set the observed property to true to trigger the next ad
self.adFinished = true
print("Ad number [\(self.currentAd)] completed")
})
}
}
//
//ViewController.swift
//图像记录器
//
//托马斯·卡罗尔于2012年5月11日创作。
//
进口可可
类ViewController:NSViewController{
var adFilesArray:Array=[]
var adImageView:Array=[]
var imageView:NSImageView!
var currentAd:Int=-1
var adFinished=false{
迪塞特{
如果完成{
//增加要显示的广告
当前AD+=1
//如果当前ad是阵列中的最后一个ad
如果currentAd==adFilesArray.count{
//重置为阵列中的第一个ad
当前AD=0
}
//将观察到的属性重置为true
adFinished=true
//重新触发showNextAd方法
shownestad()
}
}
}
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后执行任何其他设置。
//加载ad文件名数组和图像视图
做{
adFilesArray=try FileManager.default.contentsOfDirectory(路径:“/Users/Shared/Ad Images”)
对于0中的i…adFilesArray.count-1{
imageView=NSImageView(图像:NSImage(通过引用文件:“/Users/Shared/Ad-Images/”+adFilesArray[i])!)
adImageView.append(imageView)
}
}抓住{
打印(“无法检索广告图像”)
出口(1)
}
//将adFinished属性设置为true以触发ads开始
adFinished=true
}
覆盖var representedObject:有吗{
迪塞特{
//更新视图(如果已加载)。
}
}
func shownestad(){
//加载imageView
imageView=adImageView[currentAd]
//不要设置自动调整大小约束
imageView.TranslatesAutoResizezingMaskintoConstraints=false
//在屏幕左侧设置初始位置
imageView.frame=CGRect(x:-imageView.image!.size.width,y:200,width:1100,height:700)
//将图像视图添加为子视图
self.view.addSubview(imageView)
//开始动画
NSAnimationContext.runAnimationGroup({context在
//正在设置什么动画?我希望图像视图从左侧(屏幕外)滚动到右侧(屏幕外)
//指示动画的持续时间
context.duration=15.0
//将原点(目的地)的x坐标设置为屏幕宽度)
设原点=CG点(x:2201,y:200)
//将原点设置为图像将设置动画(滚动)的位置
self.view.subview[0]。animator().frame.origin=origin
},completionHandler:{
//删除子视图
self.view.subview.remove(位于:0)
//将observed属性设置为true以触发下一个ad
self.adFinished=true
打印(“广告编号[\(self.currentAd)]已完成”)
})
}
}
我如何才能使过渡始终保持相同的速度,如何消除它留下的潜在线条?找到了一个关于滚动速度的答案。我偶然发现了另一个设置
context.timingFunction=CAMediaTimingFunction(名称:.linear)
将使转换速度在动画持续时间内保持不变。现在要去除屏幕上的潜在线条。。。