首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >了解CGAffineTransform交互

了解CGAffineTransform交互
EN

Stack Overflow用户
提问于 2011-10-26 05:31:07
回答 2查看 3.1K关注 0票数 1

我正在尝试做一个简单的应用程序,在这个应用程序中,一张被“钉住”的图片在被一个手指移动后会返回到原来的位置。这可能用代码解释得更好:

代码语言:javascript
复制
- (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];

    }
}

每次我按下图像,它就会移动,这样它就会从我的手指下移出来。我认为这与转换有关。有谁能给我指个方向吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-10-26 05:44:14

编辑过的

我实际上是这样做的:

代码语言:javascript
复制
- (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];
    }
}
票数 1
EN

Stack Overflow用户

发布于 2011-10-26 06:36:38

我觉得你应该直接用

代码语言:javascript
复制
image.transform = CGAffineTransformMakeTranslation(difference.x, difference.y);

你这样做的方式是在touchesMoved的每一次迭代中积累越来越多的翻译。我认为center属性并不依赖于变换。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7896148

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档