0

i have the following situation , i'm trying to make a photo editor and i need to do scale,translate and rotate operations for an image with around center of view

the problem is that if i apply translate after 90 degree translate left is translate top and the anchor point is not the center of the view after translation

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    // ImageView
    v = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 768, 500)];
    [v setBackgroundColor:[UIColor redColor]];

    // ImageView's Image
    UIImage *img = [UIImage imageNamed:@"_my.jpg"];
    [v setImage:img];
    v.contentMode = UIViewContentModeScaleAspectFit;
    v.layer.anchorPoint = CGPointMake(0.5, 0.5);
    [self.view setBackgroundColor:[UIColor clearColor]];

    // UiView containing ImageView
    vc = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 768, 500)];
    [vc addSubview:v];
    [self.view addSubview:vc];
}

- (IBAction)Click:(id)sender {
    // Translate
    if ([sender tag] == 1) {
        CGAffineTransform t0 = v.transform;
        CGAffineTransform t1 = CGAffineTransformTranslate(t0, 10.0, 0.0);
        v.transform = t1;
    }
    // Rotate
    if ([sender tag] == 2) {
        CGAffineTransform t0 = v.transform;
        CGAffineTransform t1 = CGAffineTransformRotate(t0, DEGREES_TO_RADIANS(10));
        v.transform = t1;
    }    
}

does anyone know how can i keep the center of view as the anchor point and translate after rotation to work properly ?

tx

4

1 回答 1

0

原点转换

但是,尤其是在 2D 中,您经常需要在变换中添加原点位移。这通常被添加到开头,也必须被否定,所以:

// 带原点的世界矩阵

-OriginTranslation * Scale * Rotation * PositionTranslation

同样的事情也可以用视图矩阵来完成,但是你将它添加到最后,这次你不要否定它:

// 查看原点矩阵

-PositionTranslation * -Rotation * Zoom * OriginTranslation

具有选择性原点的转换

在某些情况下,您希望进行原点位移,但您希望它仅影响您的缩放和旋转,而不影响您的平移。处理该问题的方法是在到达翻译组件之前撤消原点转换。例如,对于世界矩阵:

我不记得,确切地说,转换是如何应用视图的,其中一个应该是有效的。

// 具有缩放和旋转原点的世界矩阵

-OriginTranslation * Scale * Rotation * OriginTranslation * PositionTranslation

和视图矩阵:

// 查看原点矩阵

-PositionTranslation * -OriginTranslation * -Rotation * Zoom * OriginTranslation

无原点变换示例,顺序:缩放 * 旋转 * 平移

在此处输入图像描述

于 2013-05-17T08:29:47.100 回答