了解 CGAffineTransform 交互

发布于 2024-12-12 02:20:03 字数 1159 浏览 1 评论 0原文

我正在尝试制作一个简单的应用程序,其中“固定”的图像在用手指移动后返回到其位置。这可能用代码可以更好地解释:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
     image.transform = CGAffineTransformIdentity;
}


- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    if (CGRectContainsPoint([image frame], [touch locationInView:nil])) 
    {
         image.center = [touch locationInView:nil];
    }
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
     if (pin) {
         CGPoint point = image.center;
         CGPoint center = self.view.center;
         //CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 0);
         [UIView beginAnimations:nil context:NULL];
         [UIView setAnimationDuration:0.5];
         image.transform = CGAffineTransformMakeTranslation(center.x - point.x, center.y - point.y);
         //image.transform = CGAffineTransformConcat(image.transform, CGAffineTransformMakeTranslation(center.x - point.x, center.y - point.y));
    [UIView commitAnimations];

    }
}

每次我按下图像时,它都会移动,以便它从我的手指下移出。我认为这与转变有关。有人能指出我正确的方向吗?

I'm trying to make a simple app where an image that is "pinned" ge's returned to its position after being moved by a finger. This is probably explained better with code:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
     image.transform = CGAffineTransformIdentity;
}


- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    if (CGRectContainsPoint([image frame], [touch locationInView:nil])) 
    {
         image.center = [touch locationInView:nil];
    }
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
     if (pin) {
         CGPoint point = image.center;
         CGPoint center = self.view.center;
         //CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 0);
         [UIView beginAnimations:nil context:NULL];
         [UIView setAnimationDuration:0.5];
         image.transform = CGAffineTransformMakeTranslation(center.x - point.x, center.y - point.y);
         //image.transform = CGAffineTransformConcat(image.transform, CGAffineTransformMakeTranslation(center.x - point.x, center.y - point.y));
    [UIView commitAnimations];

    }
}

Every time I press the image it shifts so that it moves out from under my finger. I think it has something to do with the transformations. Could anyone please point my in the right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叹倦 2024-12-19 02:20:03

我认为你应该使用

image.transform = CGAffineTransformMakeTranslation(difference.x, difference.y);

你这样做的方式,你会在每次迭代 TouchMoved 时积累越来越多的翻译。我认为中心属性不依赖于变换。

I think you should just use

image.transform = CGAffineTransformMakeTranslation(difference.x, difference.y);

The way you're doing it you're accumulating more and more translation every iteration of touchesMoved. The center property does not depend on the transform I think.

丢了幸福的猪 2024-12-19 02:20:03

编辑

我实际上会这样做:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
}


- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint location = [[touches anyObject] locationInView:[touch view]];
    CGPoint difference = CGPointMake(location.x - image.center.x, location.y - image.center.y);

    image.transform = CGAffineTransformTranslate(image.transform, difference.x, difference.y);
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if (pin) {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.5];
        image.transform = CGAffineTransformIdentity;
        [UIView commitAnimations];
    }
}

EDITED

I'd ACTUALLY do it like this:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
}


- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint location = [[touches anyObject] locationInView:[touch view]];
    CGPoint difference = CGPointMake(location.x - image.center.x, location.y - image.center.y);

    image.transform = CGAffineTransformTranslate(image.transform, difference.x, difference.y);
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if (pin) {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.5];
        image.transform = CGAffineTransformIdentity;
        [UIView commitAnimations];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文