0

我有一个 UIImageView 对象,我用 frame 属性和 CFaffineTransformMakeRotate 旋转,然后我想用它的框架的移动原点来移动它,但是我的图像移动并奇怪地重塑了形状。

 @implementation TimberView

  • (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { // Retrieve the touch point CGPoint pt = [[touches anyObject] locationInView:self]; startLocation = pt; [[self superview] bringSubviewToFront:self]; }

  • (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { // Move relative to the original touch point CGPoint pt = [[touches anyObject] locationInView:self]; CGRect frame = [self frame];

    frame.origin.x += pt.x - startLocation.x; frame.origin.y += pt.y - startLocation.y; [self setFrame:frame]; }

TimberView 类是 UIImageView 的子类

4

1 回答 1

0

引用 UIView 的 frame 属性参考:

如果还设置了 transform 属性,请改用 bounds 和 center 属性;否则,对 frame 属性的动画更改无法正确反映视图的实际位置。

因此,如果您将一些自定义转换应用于您的视图,您将无法使用视图的 frame 属性。要移动视图,请更改其center属性,因此您的代码应转换为(不确定代码是否正确):

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    // Move relative to the original touch point
    CGPoint pt = [[touches anyObject] locationInView:self];
    CGPoint newCenter;

    newCenter.x += pt.x - startLocation.x;
    newCenter.y += pt.y - startLocation.y;
    [self setCenter: newCenter];
}

如果您只想将视图的中心定位到触摸点,您可以使用以下代码:

UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView: [self superview]];
self.center = touchPoint;
于 2010-08-14T13:18:42.003 回答