- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个 UILabel 对象,它正在动画化,上下移动。我正在使用 Swift 3 在 Xcode 8.3 中编码。用户可以平移此对象并拖动到一个位置以获得一些点。我正在使用 touchesXXX
手势处理平移/拖动。然而,当我点击并立即放开它时,这个对象出于某种原因垂直跳到它的位置上方,我无法弄清楚为什么现在一个星期......
当我启用一些调试时,我只能看到 touchesBegan
被调用(touchesMoved
没有按预期调用,touchesEnded
也没有出现意外大部头书)。如果我手动禁用对象上的动画,它会按预期工作并且可以毫不费力地平移对象并且显然没有对象跳跃。
这里是相关代码的摘录:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let touchLocation = touch!.location(in: self.view)
if self.optOneLbl.layer.presentation()!.hitTest(touchLocation) != nil {
//pauseLayer(self.optOneLbl.layer) <<<<< comment #1
//optOneLbl.translatesAutoresizingMaskIntoConstraints = true <<<<< comment #2
optOneLbl.center = touchLocation
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let touchLocation = touch!.location(in: self.view)
var sender: UILabel
if self.optOneLbl.layer.presentation()!.hitTest(touchLocation) != nil {
self.optOneLbl.center = touchLocation
sender = self.optOneLbl
}
// identify which letter was overlapped
var overlappedView : UILabel
if (sender.frame.contains(letter1.center)) {
...
}
else {
return
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
}
在阅读了对其他 SO 问题的回复后,我认为按照 touchesBegan
中的上述评论 #1 以编程方式禁用标签动画可能会有所帮助,但问题仍然存在。另外,我认为可能是自动布局导致了这种奇怪的跳跃。因此,我根据评论 #2 启用了 translatesAutoresizingMaskIntoConstraints
,但它也没有帮助。
任何人都能看出我在哪里处理不当?感谢阅读!
编辑:
应@agibson007 的要求,我添加了动画代码摘录以供引用:
UIView.animate(withDuration: 12.0, delay: 0.0, options: [ .allowUserInteraction, .curveLinear, .autoreverse, .repeat ], animations: {
self.optOneLbl.center = CGPoint(x: self.optOneLbl.center.x, y: screenSize.midY*1.1)
})
最佳答案
更改标签位置后需要移除/重置动画。动画不知道您更新了值,并试图从一开始就保持在相同的 byValue 范围内。这是更新动画的工作示例。
import UIKit
class ViewController: UIViewController {
var optOneLbl = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
optOneLbl = UILabel(frame: CGRect(x: 20, y: 50, width: self.view.bounds.width - 40, height: 40))
optOneLbl.textAlignment = .center
optOneLbl.text = "I Will Be Moving Up and Down"
optOneLbl.textColor = .white
optOneLbl.backgroundColor = .blue
self.view.addSubview(optOneLbl)
//starts it in the beginnning with a y
fireAnimation(toY: self.view.bounds.midY*1.1)
}
func fireAnimation(toY:CGFloat) {
UIView.animate(withDuration: 12.0, delay: 0.0, options: [ .allowUserInteraction, .curveLinear, .autoreverse, .repeat ], animations: {
self.optOneLbl.center = CGPoint(x: self.optOneLbl.center.x, y:toY)
})
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let touchLocation = touch!.location(in: self.view)
if self.optOneLbl.layer.presentation()!.hitTest(touchLocation) != nil {
//re
optOneLbl.layer.removeAllAnimations()
optOneLbl.center = touchLocation
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let touchLocation = touch!.location(in: self.view)
var sender: UILabel
if self.optOneLbl.layer.presentation()!.hitTest(touchLocation) != nil {
self.optOneLbl.center = touchLocation
sender = self.optOneLbl
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
//restart animation after finished and change the Y if you want.
// you could change duration or whatever
fireAnimation(toY: self.view.bounds.height - optOneLbl.bounds.height)
}
}
关于ios - 调用 touchesBegan 时动画 UIView 对象跳转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45666869/
我有两个函数,在 C++ 中看起来像这样: void f1(...); void f2(...); 我可以更改 f1 的主体,但是 f2 是在另一个我无法更改的库中定义的。我绝对必须(尾部)在f1 中
我正在尝试避免这个 while 循环出了什么问题 while True: start = input("Ok, are you ready now?\n") if (start !=
当我点击按钮加载页面时,它加载正常。调整浏览器大小后,此页面会不断刷新。我复制了布局页面的一部分,用于处理按钮。如果我遗漏了什么,你能告诉我吗? Statistics 最佳答案 确保没有在“调
PHP的header函数 可以很少代码就能实现HTML代码中META 标签 这里只说用 header函数来做页面的跳转 1. HTML代码中页面的跳转的代码 HTML meta refresh 刷
我使用并喜欢 Tim Pope's excellent Fugitive plugin for VIM ,我一直希望能够加载 :Glog quickfix 中所有文件的差异,并通过它们循环到 HEAD
当我访问 my_site.com/page.php#something 时,滚动位置是携带此特定主题标签的元素之一,而不是页面顶部。 执行 window.scrollTo(0, 0); 不会改变这一事
在我自己的一个应用程序中看到这一点后,我构建了一个测试应用程序。 我在导航 Controller 中嵌入了一个集合 View 。 这一切都链接到 ViewController 类。 class Vie
在我们的应用程序中,当其中一个 View 被推送到导航 Controller 时,导航栏会显示 5 秒,然后我们将 navigationBarHidden 设置为 TRUE。稍后,如果用户点击屏幕,我
我必须更新滚动 UITextView 中的少量文本。我只会在光标当前所在的位置插入一个字符,并且只需按一下导航栏上的按钮即可执行此操作。 我的问题是,每当我调用 TextView 的setText方法
我正在使用第三方脚本(详细信息如下)将表单分解为多个 ajax 页面。当我单击前进到下一页时,它会跳转到表单顶部的 anchor 。这是一种不必要且不和谐的行为,我想阻止它发生。这个脚本不是我写的,不
这个问题已经有答案了: Call an absolute pointer in x86 machine code (2 个回答) 已关闭 4 年前。 我正在用 C 语言为 x86_64 linux 编
查看objdump -d ELFfile的输出,我无法区分直接和间接跳转/调用。有什么建议吗? 最佳答案 间接调用和跳转有*在指令之后和位置之前,如callq *%r13和jmpq *0x204d8a
我试图通过单击 anchor 链接来突出显示 div。我的意思是如果我点击My link 然后滚动到 here在此页面上,我有很多 anchor 链接和 div,因此很难识别哪个 div 然后滚动,因
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 8 年前。 Improve this ques
没有任何编程经验,只有基本的 html/css。我正在尝试创建一个 javascript 菜单(如果有更简单的方法,可以接受建议吗?),它将根据第一个菜单中选择的选项显示/隐藏?我有以下... Q
我正在使用 jquery 的 scrollTop 函数,但无法弄清楚为什么在同一项目上多次调用时它会跳来跳去。 我把这个 example 放在一起了来说明问题。单击测试按钮时,它会交替滚动到指定的项目
我已经创建了一个关于 Storyboard的项目, Storyboard上有两个名为“loginViewController”、“BViewController”的 View Controller ,
基本上,我在网站的最顶部有一个我想隐藏的 div,当您单击一个按钮时,它会将整个网站向下推并显示其内容。与此非常相似 nd.edu (单击标题右侧的帮助中心或白杨站点)。我正在使用 jquery 来完
当我单击所单击的 div 旁边的 div 框时,我的底部 div 会上下跳跃 这是 fiddle 的链接 http://jsfiddle.net/abtPH/17/ 这是我的jquery $('li
我在悬停时调整 .main-partners div 中卡片的大小,以便出现 查看更多 按钮。 我的问题是当我 :hover 在 .main-partners 中的卡片 (.main-card) 上时
我是一名优秀的程序员,十分优秀!