[英]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.