首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用CGAffineTransformScale与UIAttachmentBehavior (UIDynamicAnimator)

使用CGAffineTransformScale与UIAttachmentBehavior (UIDynamicAnimator)
EN

Stack Overflow用户
提问于 2013-11-14 03:48:06
回答 1查看 1.5K关注 0票数 4

在UIButton的子类中,我将UIButton附加到一个允许用户用手指在屏幕上拖动按钮的UIAttachmentBehavior。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event中,我将按钮添加到UIAttachmentBehavior中,然后将行为添加到UIDynamicAnimator中。在- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event期间,我将UIAttachmentBehavior的锚点更新到接触点;这将创建所需的拖动效果。

现在,我希望在触摸开始时使用CGAffineTransformScale来增加按钮的大小,这样用户就可以在他们的手指下看到按钮。,我的问题是,我在CGAffineTransformScale中应用的转换是在我添加附件行为的第二次写入后立即完成的。结果是按钮快速闪烁,但随后又返回到原来的大小。

在应用[_animator removeAllBehaviors]之前,我已经尝试过CGAffineTransformScale,然后再添加行为。在应用[_animator updateItemUsingCurrentState:self]之后,在添加附件行为之前,我也尝试过CGAffineTransformScale。这两个问题都没有解决。

更新1:考虑到HalR下面的答案,我决定尝试每一次都应用比例转换。因此,我将CGAffineTransformScale调用添加到touchesMoved:touchesEnded中。我使用CGAffineTransformScale vs CGAffineTransformMakeScale,因为它允许我保留附加行为添加的轻微旋转。让我离得更近了。该按钮现在在屏幕周围移动,同时进行缩放。但这并不完美。当你没有在屏幕周围移动时,会有一个闪烁,如果你停止移动,但保持触摸向下,按钮返回到原来的大小。几乎是there...any的建议?

下面是我的更新的代码:

代码语言:javascript
复制
@interface DragButton : UIButton < UIDynamicAnimatorDelegate >

#import "DragButton"
#import <QuartzCore/QuartzCore.h>

@implementation DragButton

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];

    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.referenceView];

    self.transform = CGAffineTransformMakeScale(1.5, 1.5);

    _touchAttachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:self attachedToAnchor:touchLocation];
    [_animator addBehavior:_touchAttachmentBehavior];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];

    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.referenceView];

    self.transform = CGAffineTransformScale(self.transform, 1.5, 1.5);

    _touchAttachmentBehavior.anchorPoint = touchLocation;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];

    self.transform = CGAffineTransformScale(self.transform, 1.5, 1.5);

    [_animator removeBehavior:_touchAttachmentBehavior];
}
EN

回答 1

Stack Overflow用户

发布于 2013-11-14 04:15:44

UIAttachmentBehavior如何影响视图?

它是通过修改视图的转换来实现的。

在此雷·温德利希教程中,Colin记录转换,因为视图受此行为的影响。

即使从未设置或直接修改转换,但随着视图被该行为移动,它也会发生变化。

所以您不能设置您的转换,并希望它保持不变,因为它是由行为设置的。

在附带说明中,如果您试图将转换设置为比例为1.5,则应使用以下命令:

代码语言:javascript
复制
self.transform = CGAffineTransformMakeScale(1.5, 1.5);

否则,每次调用您的touchesBegan,它都会再增长50%。

在UIDynamicAnimator的文档中,它说:

“动态动画器自动读取添加到其中的每个动态项的初始状态(位置和旋转),然后负责更新项目的状态。如果在将动态项添加到动态动画器后主动更改动态项的状态,请调用此方法要求动画师读取并合并新状态。”

因此,只需在添加行为之后调用您的转换,然后调用updateItemUsingCurrentState:

代码语言:javascript
复制
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];

    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.referenceView];

    _touchAttachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:self attachedToAnchor:touchLocation];
    [_animator addBehavior:_touchAttachmentBehavior];
    self.transform = CGAffineTransformMakeScale(1.5, 1.5);
    [_animator updateItemUsingCurrentState:self];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19969185

复制
相关文章

相似问题

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