滚动时旋转视图

发布于 2024-11-27 14:16:45 字数 110 浏览 0 评论 0原文

非常简单的问题:

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

塔塔猫 2024-12-04 14:16:45

实现 UIScrollViewDelegate 并使用仿射变换进行旋转。

#import <QuartzCore/QuartzCore.h>

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint offset = scrollView.contentOffset;
    CGFloat rotateDegrees = offset.y;
    CGFloat rotateRadians = rotateDegrees * (M_PI / 180);
    _myImage.layer.affineTransform = CGAffineTransformMakeRotation(rotateRadians);
}

要重置旋转,请使用_myImage.layer.affineTransform = CGAffineTransformIdentity

Implement UIScrollViewDelegate and rotate with an affine transform.

#import <QuartzCore/QuartzCore.h>

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint offset = scrollView.contentOffset;
    CGFloat rotateDegrees = offset.y;
    CGFloat rotateRadians = rotateDegrees * (M_PI / 180);
    _myImage.layer.affineTransform = CGAffineTransformMakeRotation(rotateRadians);
}

To reset the rotation, use _myImage.layer.affineTransform = CGAffineTransformIdentity.

小霸王臭丫头 2024-12-04 14:16:45

为滚动分配 代理查看并实现 -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 the contentOffset 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's transform property.

聽兲甴掵 2024-12-04 14:16:45

垂直滚动和 Swift 5:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let topOffset = scrollView.contentOffset.y
    let angle = -topOffset * 2 * CGFloat(Double.pi / 180)
    self.myImageView.transform = CGAffineTransform(rotationAngle: angle)
}

是的,不要忘记在 viewDidLoad 中执行此操作:

self.scrollView.delegate = self

Vertical scrolling and Swift 5:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let topOffset = scrollView.contentOffset.y
    let angle = -topOffset * 2 * CGFloat(Double.pi / 180)
    self.myImageView.transform = CGAffineTransform(rotationAngle: angle)
}

And, yeah, don't forget to do this in viewDidLoad:

self.scrollView.delegate = self
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文