【问题标题】:How to rotate a line (UIView) around it's origin如何围绕它的原点旋转一条线(UIView)
【发布时间】:2013-11-01 01:55:21
【问题描述】:

我正在尝试围绕它的原点旋转一条线(只不过是一个 UIView)。对于旋转,我使用CGAffineTransformRotate,但我知道这会围绕它的中心旋转线。要更改我使用CGAffineTransformTranslate 的旋转点。尽管如此,这条线没有正确旋转。我究竟做错了什么?这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(300, 400, 300, 3)];
    testView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:testView];

    testView.transform = CGAffineTransformTranslate(testView.transform, -150, 0);
    testView.transform = CGAffineTransformRotate(testView.transform, 45 * M_PI / 180);
}

提前致谢

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    我以前也遇到过这种情况,答案并不明显,但很简单。问题是您没有恢复视图的“旋转点”。出于某种原因,这会使轮换无法按您想要的方式进行。

    您必须采取以下措施来解决问题:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(300, 400, 300, 3)];
        testView.backgroundColor = [UIColor blueColor];
        [self.view addSubview:testView];
    
        testView.transform = CGAffineTransformTranslate(testView.transform, -150, 0);
        testView.transform = CGAffineTransformRotate(testView.transform, 45 * M_PI / 180);
    
        // Restore the rotation point. This will fix your issue.
        testView.transform = CGAffineTransformTranslate(testView.transform, 150, 0);
    }
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-03
      • 2019-06-28
      • 1970-01-01
      • 2019-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多