0

フレームプロパティとCFAffineTransformMakeRotateを使用して回転するUIImageViewオブジェクトがあり、フレームの原点を移動して移動したいのですが、画像が移動して変形します。

 @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プロパティへの変更をアニメーション化しても、ビューの実際の場所が正しく反映されません。

したがって、ビューにカスタム変換を適用すると、ビューの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 に答える