设置CGAffineTransformMakeRotation斯威夫特一个旋转点 [英] Setting a rotation point for CGAffineTransformMakeRotation Swift

查看:243
本文介绍了设置CGAffineTransformMakeRotation斯威夫特一个旋转点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是code下面旋转的UIImageViews以不同的角度。

I am using the code below to rotate UIImageViews to different angles.

self.imageOne.transform = CGAffineTransformMakeRotation(CGFloat(-rotation))
self.imageTwo.transform = CGAffineTransformMakeRotation(CGFloat(-rotation-0.1))
self.imageThree.transform = CGAffineTransformMakeRotation(CGFloat(-change+0.1))

有什么办法让它尽量从上而不是中间的图像支点吗我想避免卷入,因为我从来没有使用过的精灵,不知道怎么样使在单个视图应用程序精灵的工作。谢谢你的时间。

Is there any way to make it so the images pivot from the top instead of the middle? I would like to avoid getting sprites involved because I have never used them, and don't know how to make sprites work in a single view application. Thanks for your time.

推荐答案

什么是你正在寻找的<一个href=\"https://developer.apple.com/library/mac/Documentation/GraphicsImaging/Reference/CALayer_class/index.html#//apple_ref/occ/instp/CALayer/anchorPoint\">anchorPoint你的观点图层属性。这个属性基本上重新presents的的处理的同时查看正在四处移动所使用。

What you are looking for is the anchorPoint property of your views' layers. This property basically represents the handle that is be used while the view is being moved around.

它默认视图的中心了,那为什么你总是看到您的视图旋转形式的中间。

It defaults to the center of your view so and that why you are always seeing your view rotating form the middle.

您可以使用这个伟大的方法我已经在这里找到了SO改变你的意见anchorPoint顶端(使用CGPointMake(0,0.5F))

You can use this great method i've found here on SO to change the anchorPoint of you views to the top (use CGPointMake(0, 0.5f))

// Swift version
// Place this before you set the transform property
setAnchorPoint(CGPointMake(0, 0.5), view: imageOne)
setAnchorPoint(CGPointMake(0, 0.5), view: imageTwo)
setAnchorPoint(CGPointMake(0, 0.5), view: imageThree)

func setAnchorPoint(anchorPoint: CGPoint, view: UIView) {
    var newPoint = CGPointMake(view.bounds.size.width * anchorPoint.x, view.bounds.size.height * anchorPoint.y)
    var oldPoint = CGPointMake(view.bounds.size.width * view.layer.anchorPoint.x, view.bounds.size.height * view.layer.anchorPoint.y)

    newPoint = CGPointApplyAffineTransform(newPoint, view.transform)
    oldPoint = CGPointApplyAffineTransform(oldPoint, view.transform)

    var position : CGPoint = view.layer.position

    position.x -= oldPoint.x
    position.x += newPoint.x;

    position.y -= oldPoint.y;
    position.y += newPoint.y;

    view.layer.position = position;
    view.layer.anchorPoint = anchorPoint;
}

这是相同code的客观C版

This is the objective c version of the same code

// Obj-c version
// Place this before you set the transform property
[self setAnchorPoint:CGPointMake(0, 0.5f) forView:imageOne];
[self setAnchorPoint:CGPointMake(0, 0.5f) forView:imageTwo];
[self setAnchorPoint:CGPointMake(0, 0.5f) forView:imageThree];

-(void)setAnchorPoint:(CGPoint)anchorPoint forView:(UIView *)view
{
    CGPoint newPoint = CGPointMake(view.bounds.size.width * anchorPoint.x,
                                   view.bounds.size.height * anchorPoint.y);
    CGPoint oldPoint = CGPointMake(view.bounds.size.width * view.layer.anchorPoint.x,
                                   view.bounds.size.height * view.layer.anchorPoint.y);

    newPoint = CGPointApplyAffineTransform(newPoint, view.transform);
    oldPoint = CGPointApplyAffineTransform(oldPoint, view.transform);

    CGPoint position = view.layer.position;

    position.x -= oldPoint.x;
    position.x += newPoint.x;

    position.y -= oldPoint.y;
    position.y += newPoint.y;

    view.layer.position = position;
    view.layer.anchorPoint = anchorPoint;
}

我鼓励你去尝试了解自己是什么,一旦你已经看到了anchorPoint产权重组presents这是什么code在做什么。 (该方法是通过用户:阿南德ķ

这篇关于设置CGAffineTransformMakeRotation斯威夫特一个旋转点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆