- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
在 UIButton 的子类中,我将 UIButton 附加到 UIAttachmentBehavior,它允许用户用手指在屏幕上拖动按钮。
在 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
中,我将按钮添加到 UIAttachmentBehavior,然后将行为添加到 UIDynamicAnimator。在 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
期间,我将 UIAttachmentBehavior 的 anchor 更新为触摸点;这会产生所需的拖动效果。
现在我希望在触摸开始时使用 CGAffineTransformScale 来增加按钮的大小,以便用户可以看到他们手指下的按钮。 我的问题是,我使用 CGAffineTransformScale 应用的转换在我添加附件行为后立即被覆盖。结果是按比例放大的按钮快速闪烁,但随后返回到原始大小。
在应用 CGAffineTransformScale 之前,我尝试了 [_animator removeAllBehaviors]
,然后将行为添加回来。我还尝试了 [_animator updateItemUsingCurrentState:self]
在应用 CGAffineTransformScale 之后,就在添加附件行为之前。都无法解决问题。
更新 1:考虑到 HalR 在下面的回答,我决定尝试在每次触摸时应用缩放变换。因此,我将 CGAffineTransformScale 调用添加到 touchesMoved:
和 touchesEnded
。我正在使用 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
如何影响 View ?
它通过修改 View 的转换来实现。
在此Ray Wenderlich tutorial Colin Eberhardt 在 View 受行为影响时记录转换。
即使变换从未设置或直接修改,它也会随着 View 被行为移动而改变。
所以你不能设置你的转换,并希望它保持设置,因为它是由行为设置的。
在旁注中,如果您尝试将变换设置为按 1.5 缩放,您应该使用:
self.transform = CGAffineTransformMakeScale(1.5, 1.5);
否则,每次调用您的 touchesBegan
时,它都会再增长 50%。
在 UIDynamicAnimator 的文档中,它说:
"A dynamic animator automatically reads the initial state (position and rotation) of each dynamic item you add to it, and then takes responsibility for updating the item’s state. If you actively change the state of a dynamic item after you’ve added it to a dynamic animator, call this method to ask the animator to read and incorporate the new state."
所以只需在添加行为后调用您的转换,然后调用 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];
}
关于ios - 将 CGAffineTransformScale 与 UIAttachmentBehavior (UIDynamicAnimator) 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19969185/
我有一个 3x3 的 UIView 网格,其中添加了 UIGestureRecognizers,排列如下: 它的工作方式是,点击一个正方形,使用 CGAffineTransformScale 将其放大
我正在创建一个故事书应用,它本质上是一系列短动画。每个“翻页”只是页面缩小的简短动画。对于 page1,动画按预期工作,但是对于 page2,如果我在第一个动画仍在进行时调用操作 page2,它似乎会
我在 viewDidLoad 中使用这段代码来缩放我的 UIImageView: imageView.transform = CGAffineTransformScale(CGAffineTransf
我是 Monotouch 的新手,任何人都可以回答我需要做什么才能在 C# 中使用 CGAffineTransformScale 方法。我添加了结构(使用 MonoTouch.CoreGraphics
我有一个简单的应用程序,它将 ImageView 放置在屏幕上,并允许用户使用捏合和平移手势识别器来移动和缩放它。在我的 uipangesturerecognizer 委托(delegate)方法(s
我只是想了解使用 updateItemUsingCurrentState 的方式以及如何将缩放的 UIView 或 UIImageView 发送给动画师,这样我就可以在 viewDidLoad 上有一
假设我像这样使用 CGAffineTransformScale 缩放 UILabel: let scale = 0.5 text = UILabel(frame: CGRectMake(100, 10
我正在尝试使用 animate(withDuration:animations:) 为 Playground 中的 UIView 设置动画.目标是将 View 向下移动,然后向右移动,同时将其放大。
我正在学习为 iOS 开发,作为教育过程的一部分,我创建了自定义 UIView,它使用 drawRect 来绘制它的内容(Core Graphics + UIBezierPath 基于)。一切都按预期
Swift 3.0 没有名为 CGAffineTransformScale 的方法。 func didPinchGesture(pinchRecognizer : UIPinchGestureReco
在 UIButton 的子类中,我将 UIButton 附加到 UIAttachmentBehavior,它允许用户用手指在屏幕上拖动按钮。 在 - (void)touchesBegan:(NSSet
考虑一个简单的 Xcode 6 Swift“单 View ”项目。自动布局已停用。 View (所有自动调整已停用)包含一个 100x100 UIImageView(图像)和两个按钮“移动”和“调整大
我认为我的问题可以概括为如何存储和重置 View 的变换。但也许解释我的情况可能会有所帮助。 如果我将下面的转换一个接一个地应用到 View (就像我将此代码添加到开关或按钮)。我得到了我期望的结果:
如果我这样做: view.transform = CGAffineTransformScale(view.transform, s, s); 我的 View 已缩放,但它的 subview 也已缩放.
我在 viewcontroller 中有一个 UITableView,想在 viewcontroller 的中心缩小。它在 ios7 中工作正常,代码如下: CGSize appSize = [[UI
我是一名优秀的程序员,十分优秀!