Ios 将CGAffineTransformScale与UIAttachmentBehavior(UIDynamicMator)一起使用

Ios 将CGAffineTransformScale与UIAttachmentBehavior(UIDynamicMator)一起使用,ios,iphone,objective-c,uikit-dynamics,uidynamicbehavior,Ios,Iphone,Objective C,Uikit Dynamics,Uidynamicbehavior,在UIButton的子类中,我将UIButton附加到UIAttachmentBehavior,该行为允许用户用手指在屏幕上拖动按钮 在-(void)touchsbegind:(NSSet*)touchs with event:(UIEvent*)event中,我将按钮添加到UIAttachmentBehavior,然后将该行为添加到UIDynamicMator。在-(void)touchsmoved:(NSSet*)touchs with event:(UIEvent*)event期间,我将U

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

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

现在,我希望使用CGAffineTransformScale在触摸开始时增加按钮的大小,以便用户可以在手指下看到按钮我的问题是,在我添加附件行为的那一刻,我使用CGAffineTransformScale应用的转换立即被重写。结果是按钮快速闪烁放大,但随后又返回到原始大小。

在应用CGAffineTransformScale之前,我已经尝试了
[\u animator removeAllBehaviors]
,然后将行为添加回来。在应用CGAffineTransformScale之后,就在添加附件行为之前,我还尝试了
[\u animator UpdateTimeSingCurrentState:self]
。两者都不能解决问题

更新1:考虑到HalR下面的答案,我决定尝试在每次触摸时应用缩放变换。因此,我向
touchesMoved:
touchesend
添加了CGAffineTransformScale调用。我使用的是CGAffineTransformScale vs 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];
}
@界面拖动按钮:UIButton
#导入“拖动按钮”
#进口
@实现拖拽按钮
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件
{
[超级触摸开始:触摸事件:事件];
UITouch*touch=[[event AllTouchs]anyObject];
CGPoint touchLocation=[touch locationInView:self.referenceView];
self.transform=CGAffineTransformMakeScale(1.5,1.5);
_touchAttachmentBehavior=[[UIAttachmentBehavior alloc]initWithItem:self-attachedToAnchor:touchLocation];
[_AnimatorAddBehavior:_touchAttachmentBehavior];
}
-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件
{
[超级触摸移动:触摸事件:事件];
UITouch*touch=[[event AllTouchs]anyObject];
CGPoint touchLocation=[touch locationInView:self.referenceView];
self.transform=CGAffineTransformScale(self.transform,1.5,1.5);
_touchAttachmentBehavior.anchorPoint=touchLocation;
}
-(void)touchesend:(NSSet*)toucheevent:(UIEvent*)event
{
[超级触控:触控事件:事件];
self.transform=CGAffineTransformScale(self.transform,1.5,1.5);
[_AnimatorRemoveBehavior:_touchAttachmentBehavior];
}

UIAttachmentBehavior如何影响视图

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

在本文中,Colin Eberhardt在视图受行为影响时记录转换

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

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

在旁注中,如果尝试将变换设置为“按1.5缩放”,则应使用以下方法:

self.transform = CGAffineTransformMakeScale(1.5, 1.5);
否则,每次调用您的
touchsbegind
时,它将再增长50%

在UIDynamicMator的文档中,其说明如下:

“动态动画师会自动读取初始状态(位置) 和旋转),然后 负责更新项目状态。如果您积极更改 将动态项添加到动态项后的状态 animator,调用此方法要求animator阅读并合并 新国家。”

因此,只需在添加行为后调用转换,然后调用UpdateTimeSingCurrentState:

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

右,UIAttachmentBehavior修改转换。但是,为什么我不能告诉它“修改修改的”变换?我对
updateTimeSingCurrentState:
文档的解释是,它应该允许我主动修改比例<代码>动态动画师自动读取添加到其中的每个动态项目的初始状态(位置和旋转),然后负责更新项目的状态。如果在将动态项目添加到动态动画师后主动更改其状态,请调用此方法要求动画师读取并合并新状态。BTW,感谢CGAffineTransformMakeScale的提示…很好的调用。您是否尝试将UIImageView子视图添加到按钮中,将变换应用于该子视图的层,从而使您的行为按按钮的方式进行,然后子视图将其变换应用于该层之上?