根据superview中的位置旋转UIView

时间:2011-09-02 11:22:59

标签: objective-c ipad

我有一张UIImageView箭头图片。我将此视图添加到具有500x500维度框架的父视图中。我想旋转UIImageView,以便当用户触摸并移动它时,箭头始终指向其父级的中心。

我正在研究CGAffineTransform和三角旋转,但我开始时遇到了麻烦。任何人都可以提供一些见解吗?

谢谢!

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {    
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];

    pointerView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pointer.png"]];        
    pointerView.layer.anchorPoint = CGPointMake(0, 0.5);    
    pointerView.center = point;

    [self setPointerRotation];

    [self addSubview:pointerView];
    [pointerView release];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {    
    UITouch *touch = [touches anyObject];   
    CGPoint point = [touch locationInView:self];   

    pointerView.center = point;
    [self setPointerRotation];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [pointerView removeFromSuperview];
    pointerView = nil;
}

- (void)setPointerRotation {
    CGAffineTransform transform = // calculate rotation so that pointerView points to the center of the superview
    pointerView.transform = transform;
}

0 个答案:

没有答案