UIView:旋转+运动 [英] UIView: rotation + movement

查看:161
本文介绍了UIView:旋转+运动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试移动和旋转UIImageView。我没有使用Core Animation,也没有使用它。



顺便说一下,动画的核心部分如下:

  CGRect r = self.frame; 
r.origin.y - = self.gravity + spring;
r.origin.x - = 1;
self.rotation - = 0.015;
self.transform = CGAffineTransformMakeRotation(旋转);
self.frame = r;

它几乎可以运作。我确实移动了它或者它旋转,但是当我尝试移动旋转它时,它会完全扭曲变形。



我试图移动然后旋转而反之亦然。我也尝试了内容模式的各种值,没有运气。有什么帮助吗?



编辑



有用的教程: http://www.permadi.com/blog/2009/ 05 / iphone-sdk-bouncing-and-rotating-ball-example /

解决方案

很难确定没有您所看到的图片会发生什么,但以下内容可能会有用:



将旋转应用于视图时,它会将其旋转(0,0 ),因此您需要确保设置视图的边界,以便(0,0)是您想要旋转中心的位置。假设您正在旋转的图像是100x200图像,并且您想围绕图像的中心旋转,那么您需要说 self.bounds = CGMakeRect(-50,-100,100,200); 如果你不这样做,它将围绕它的左上角旋转,形成一个螺旋形。



哦,并且使用 self.center 设置视图的位置似乎比应用旋转时使用 self.frame 更容易预测。 / p>

这里有一些可能让它更容易理解的演示代码。在这种情况下,执行动画的代码是视图控制器,但您应该明白这一点:

  #define USE_SELF_CENTER 0 

- (void)viewDidLoad {
[super viewDidLoad];
self.rotation = 0.0f;
self.translation = CGPointMake(self.view.bounds.size.width / 2.0f,0.0);

ObjectView * ov = [[ObjectView alloc] initWithFrame:CGRectMake(0.0f,0.0f,100.0f,100.0f)];
#if USE_SELF_CENTER == 1
ov.center = self.translation;
#else
ov.transform = CGAffineTransformMakeTranslation(self.translation.x,self.translation.y);
#endif
//调整旋转中心的边界
ov.bounds = CGRectMake(-50.0f,-50.0f,100.0f,100.0f);
[self.view addSubview:ov];
self.objectView = ov;
[ov release];

NSTimer * aTimer = [NSTimer timerWithTimeInterval:0.5 target:self selector:@selector(nextFrame)
userInfo:nil repeats:YES];
self.timer = aTimer;
[aTimer release];
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}



- (无效)nextFrame
{
NSLog(@nextFrame);
self.rotation + =(5.0 / 180.0f * M_PI);
self.translation = CGPointMake(self.translation.x,self.translation.y + 2.0);

#if USE_SELF_CENTER == 1
CGAffineTransform t = CGAffineTransformMakeRotation(self.rotation);
self.objectView.center = self.translation;
self.objectView.transform = t;
#else
CGAffineTransform rot = CGAffineTransformMakeRotation(self.rotation);
CGAffineTransform txy = CGAffineTransformMakeTranslation(self.translation.x,self.translation.y);
CGAffineTransform t = CGAffineTransformConcat(rot,txy);
self.objectView.transform = t;
#endif
}

代码演示了两种做你想做的事情的方法。如果 USE_SELF_CENTER 为零,则所有动画都通过变换完成。如果 USE_SELF_CENTER 非零,则使用转换进行旋转,并通过设置视图的中心来完成转换。


I'm trying to move and rotate a UIImageView. I'm not using Core Animation for this nor I can use it.

By the way, the core part of the animation is what follows:

CGRect r = self.frame;
r.origin.y -= self.gravity + spring;
r.origin.x -= 1;
self.rotation -= 0.015;
self.transform = CGAffineTransformMakeRotation(rotation);
self.frame = r;

It almost works. I does move or it does rotate in place, but when I try to move and rotate it, it completely deforms skewing.

I've tried to move and then rotate and viceversa. I also tried various values for content mode with no luck. Any help?

EDIT

useful tutorial: http://www.permadi.com/blog/2009/05/iphone-sdk-bouncing-and-rotating-ball-example/

解决方案

It's hard to be sure what's going on without a picture of what you are seeing, but the following may be useful:

When you apply a rotation to a view it rotates it around (0,0), so you need to make sure you set the bounds of your view so that (0,0) is where you want the center of rotation. Suppose the image you are rotating is a 100x200 image and you want to rotate around the center of the image then you would need to say self.bounds = CGMakeRect(-50, -100, 100, 200); If you don't do this it will rotate about it's upper-left corner making sort of a spiral.

Oh, and using self.center to set the view's position seems to be more predictable than using self.frame when you have applied a rotation.

Here's some demo code that might make it easier to understand. In this case the code that does the animation is the view controller, but you should get the idea:

#define USE_SELF_CENTER 0

- (void)viewDidLoad {
    [super viewDidLoad];
    self.rotation = 0.0f;
    self.translation = CGPointMake(self.view.bounds.size.width/2.0f, 0.0);

    ObjectView* ov = [[ObjectView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
#if USE_SELF_CENTER==1
    ov.center = self.translation;
#else
    ov.transform = CGAffineTransformMakeTranslation(self.translation.x, self.translation.y);
#endif
    // adjust bounds for center of rotation
    ov.bounds = CGRectMake(-50.0f, -50.0f, 100.0f, 100.0f);
    [self.view addSubview:ov];
    self.objectView = ov;
    [ov release];

    NSTimer *aTimer = [NSTimer timerWithTimeInterval:0.5 target:self selector:@selector(nextFrame)
                                            userInfo:nil repeats:YES];
    self.timer = aTimer;
    [aTimer  release];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}



- (void)nextFrame
{
    NSLog(@"nextFrame");
    self.rotation += (5.0 / 180.0f * M_PI);
    self.translation = CGPointMake(self.translation.x, self.translation.y + 2.0);

#if USE_SELF_CENTER==1
    CGAffineTransform t = CGAffineTransformMakeRotation(self.rotation);
    self.objectView.center = self.translation;
    self.objectView.transform = t;
#else
    CGAffineTransform rot = CGAffineTransformMakeRotation(self.rotation);
    CGAffineTransform txy = CGAffineTransformMakeTranslation(self.translation.x, self.translation.y);
    CGAffineTransform t = CGAffineTransformConcat(rot, txy);
    self.objectView.transform = t;
#endif
}

The code demonstrates two ways of doing what you want. If USE_SELF_CENTER is zero then all the animation is done via transforms. If USE_SELF_CENTER is non-zero a transform is used for rotation and the translation is accomplished by setting the center of the view.

这篇关于UIView:旋转+运动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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