滚动时旋转视图
非常简单的问题:
我有一个 ScrollView 和一个 ImageView。对于滚动的每个像素,ImageView 应旋转一定的角度。
有什么想法如何做到这一点? 提前致谢
Pretty simple Question:
I have a ScrollView and an ImageView. For every pixel scrolled, the ImageView should rotate a certain amount of degrees.
Any ideas how to do this?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实现 UIScrollViewDelegate 并使用仿射变换进行旋转。
要重置旋转,请使用
_myImage.layer.affineTransform = CGAffineTransformIdentity
。Implement
UIScrollViewDelegate
and rotate with an affine transform.To reset the rotation, use
_myImage.layer.affineTransform = CGAffineTransformIdentity
.为滚动分配 代理查看并实现
-scrollViewDidScroll:
方法。每次滚动视图移动时都会调用此函数。在该方法中,获取滚动视图的contentOffset
,并将其与某些保存的初始状态进行比较。这可以让你得到滚动的像素/点的数量。计算出您想要将图像旋转多少,然后使用 CGAffineTransformMakeRotation 创建旋转变换 (docs),并将其分配给图像视图的
transform
属性。Assign a delegate for the scroll view and implement the
-scrollViewDidScroll:
method. This gets called every time the scroll view moves. In that method, get thecontentOffset
of the scroll view, and compare it to some saved initial state. That gets you the number of pixels/points scrolled.Figure out how much you want to rotate the image as a result, and create a rotation transform with
CGAffineTransformMakeRotation
(docs), and assign it to the image view'stransform
property.垂直滚动和 Swift 5:
是的,不要忘记在 viewDidLoad 中执行此操作:
Vertical scrolling and Swift 5:
And, yeah, don't forget to do this in
viewDidLoad
: