繁体   English   中英

将CGAffineTransformScale与UIAttachmentBehavior一起使用(UIDynamicAnimator)

[英]Using CGAffineTransformScale with UIAttachmentBehavior (UIDynamicAnimator)

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

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event我将按钮添加到UIAttachmentBehavior,然后将行为添加到UIDynamicAnimator。 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event我将UIAttachmentBehavior的锚更新到触摸点; 这会产生所需的拖动效果。

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

我在应用CGAffineTransformScale之前尝试了[_animator removeAllBehaviors] ,然后再添加行为。 在添加附件行为之前,我还在应用CGAffineTransformScale之后尝试了[_animator updateItemUsingCurrentState:self] 既不解决问题。

更新1 :考虑下面HalR的答案,我决定尝试每次触摸应用比例变换。 所以,我在touchesMoved:touchesEnded添加了CGAffineTransformScale调用。 我正在使用CGAffineTransformScale与CGAffineTransformMakeScale,因为它允许我保留附件行为添加的轻微旋转。 它让我更加接近。 按钮现在在缩放时在屏幕上移动。 虽然它并不完美。 当您没有在屏幕上移动时有一个闪烁,如果您停止移动,但保持触摸,按钮将返回到原始大小。 差不多......有什么建议吗?

这是我更新的代码:

@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];
}

UIAttachmentBehavior如何影响视图?

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

在这个Ray Wenderlich教程中, Colin Eberhardt记录了转换,因为视图受到行为的影响。

即使从未设置或直接修改转换,它也会随着行为移动视图而更改。

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

在旁注中,如果您尝试将变换设置为1.5,则应使用此:

self.transform = CGAffineTransformMakeScale(1.5, 1.5);

否则每次你的touchesBegan被称为它将再增长50%。

在UIDynamicAnimator的文档中,它说:

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

所以只需在添加行为后调用您的转换,然后调用updateItemUsingCurrentState:

- (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];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM