- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有以下屏幕:
当我点击获取更多按钮时,它会添加新的圆形按钮。我在 UIView
上添加了 Pan Gesture,其中添加了所有圆形按钮,然后仅从圆形按钮绘制 UIBezierPath
。到目前为止,一切正常。
现在,我想要以下内容:
UIView
,它包含所有圆形按钮。这将为所有这些按钮绘制一条贝塞尔曲线路径。PlayGround UIView 子类引用以下代码:
@interface PlayGroundView : UIView
@property (nonatomic, weak) PlayGroundViewController *playViewController;
@end
#import "PlayGroundView.h"
#import "RoundedButton.h"
#import "PlayGroundViewController.h"
@interface PlayGroundView () {
UIBezierPath *path;
BOOL isDrawPointInside;
}
@end
@implementation PlayGroundView
#pragma mark - View Methods -
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder])
[self commonInit];
return self;
}
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame])
[self commonInit];
return self;
}
- (void)drawRect:(CGRect)rect {
[[UIColor redColor] setStroke];
[path stroke];
}
#pragma mark - Action Methods -
- (void)pan:(UIPanGestureRecognizer *)pan {
CGPoint currentPoint = [pan locationInView:self];
// NSLog(@"currentPoint: %@", NSStringFromCGPoint(currentPoint));
if (pan.state == UIGestureRecognizerStateBegan) {
NSMutableArray *buttonAry = [NSMutableArray array];
buttonAry = self.playViewController.rBtnOpponentPlayerList;
[buttonAry addObjectsFromArray:self.playViewController.rBtnPlayerList];
for (int i=0; i<buttonAry.count; i++) {
UIButton *btn = [buttonAry objectAtIndex:i];
CGRect nearByRect = CGRectMake(btn.frame.origin.x - 20, btn.frame.origin.y - 20, btn.frame.size.width + 40, btn.frame.size.height + 40);
if (CGRectContainsPoint(nearByRect, currentPoint)) {
isDrawPointInside = YES;
[path moveToPoint:btn.center];
// NSLog(@"point is inside....");
}
}
}
if (pan.state == UIGestureRecognizerStateChanged) {
if (isDrawPointInside) {
[path addLineToPoint:currentPoint];
[self setNeedsDisplay];
}
}
if (pan.state == UIGestureRecognizerStateEnded) {
isDrawPointInside = NO;
// NSLog(@"pan ended");
}
}
#pragma mark - Helper Methods -
- (void)commonInit {
path = [UIBezierPath bezierPath];
path.lineWidth = 3.0;
// Capture touches
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
pan.maximumNumberOfTouches = pan.minimumNumberOfTouches = 1;
[self addGestureRecognizer:pan];
// Erase with long press
[self addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(erase)]];
}
- (void)erase {
path = [UIBezierPath bezierPath];
path.lineWidth = 3.0;
[self setNeedsDisplay];
}
@end
我仍然无法管理所有圆形按钮状态。让我知道在移动时管理圆形按钮所有步骤的最佳方法。
我已经提到:http://nachbaur.com/blog/core-animation-part-4
我的完整演示代码位于:https://www.dropbox.com/s/f0ri0bff8ioxtpz/FreeHandDrawingDemo%202.zip?dl=0
最佳答案
我假设,当你说“管理所有圆角按钮状态”时,你的意思是你想知道 View 已经开始动画,类似于 viewHasMovedABitAutomatically
回调:)如果是这样的话,据我所知,在 CA 框架中没有实现的方法来这样做。但是,看看这个问题:Core animation progress callback这是一种看起来很不错的解决方法,也许您可以从这里开始做一些事情。
哦,还有一件事。我看到您正在使用 Pan 手势识别器。我刚刚浏览了您的代码和 tarmes(另一个线程中的人 ;)),但我认为他正在使用 drawInContext
来检查正在绘制的图层。如果您的 View 是可拖动的,这也可能会触发,因此,如果是这种情况,请考虑在平移方法上使用一些 viewIsBeingDragged
标志,并在回调中检查它。
编辑:抱歉,这似乎与您的实际问题无关。我明白你的意思是控制动画速度所以他们都相应地移动。让我们详细说明一下。
据我所知,没有动画“步骤”这样的东西。动画图像将其向左移动 20 像素并不一定意味着图像将被重绘 20 次。图像将根据需要多次渲染,因此我们不能依赖渲染方法来计算它。据我所知,控制动画速度的唯一方法是通过 animateWithDuration:
的 duration
参数。你不知道持续时间,你想让速度恒定,所以持续时间是任何时间。
基础物理:持续时间等于距离除以速度,因此如果我们有需要覆盖的距离,我们可以轻松计算出动画持续时间。起初我以为 UIBezierPath 可能有一个方法或一个属性来计算总长度,但我错了。但是,在这个线程中 Get CGPath total length有一个代码是通过数值积分来计算它的,你应该测试它看看它是否有效。一旦你有了它,你就可以做这样的事情(我没有测试它,我只是输入伪代码)
#define SPEED_CALIBRATION 30.0 // or whatever
-(void)animateView:(UIView*)view withBezierPath:(UIBezierPath*)path {
CGFloat distance = [self getBezierPathDistance:path];
CGFloat duration = distance / SPEED_CALIBRATION;
[self animateView:view withDuration:duration andBezierPath:path];
}
是这样的。 getBezierPathDistance
是上述线程中的方法,假设它有效,animateView:withDuration:andBezierPath:
是您使用 CA 文档执行非线性动画的方法(我不太记得所有的细节,但我记得那是一段很长的代码 ;))。
很抱歉,如果答案有点含糊,希望对您有所帮助!
关于ios - 多个 UIView 的动画与 UIBezierPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29469326/
我有一个 uiview,其中包含其他自定义绘制的 uiview。现在我希望,当用户单击此父 View 时,它将扩展到带有动画的新定义的框架。我使用动画 block 取得了一些成功。问题是展开的 Vie
我正在创建当前显示的 UIView 的屏幕截图。在此过程中,我遇到了控件扭曲和拉伸(stretch)片刻(0.2-0.5 秒)的问题。它仅适用于 iPhone 6、6+。 我的简单代码在这里 (Xam
我想让主 UIView 负责将多个 UIView 之一添加为 subview 。由于任何时候只有一个 subview 处于事件状态,而 subview 完全覆盖了主视图,所以我还不如更换主视图。但这需
这真的只是好奇,但是标签属性有最大值吗?我假设标签是一个 UINT,但我不确定。 最佳答案 “标签”属性是一个 NSInteger。来自 Apple 的 docs : NSInteger Used t
我的布局中有一个 UIView 以便进行一些剪辑和分组,但是自动布局会在缩小时调整它的大小。我想给它一个固定的高度,但唯一的选择是设置顶部和底部空间。 有没有办法设置明确的高度约束? 最佳答案 我刚刚
使用 NSLayoutConstraint类,可以创建基于 View 基线 ( NSLayoutAttributeBaseline ) 的约束。但是,我还没有看到任何描述 UIView 的文档。实际上
我正在尝试创建一个 UIView,它显示一个半透明的圆圈,其边界内有一个不透明的边框。我希望能够通过两种方式更改边界 - 在 -[UIView animateWithDuration:animatio
我目前正在 iOS 中开发一个项目(使用 XCode 和 Swift)。我正在尝试为登录 View 实现以下 UITextFields: 我正在考虑不同的方法来做这件事,但它们看起来都很复杂。如果有人
我正在使用最新的 SDK 开发 iOS 应用程序。 我创建了一个自定义 UIView,我需要向该 UIView 添加一些控件。我也在使用 storyboards,并且我必须向其中添加这个 UIView
我已经通过界面生成器在 super View 中启用了Autoresize Subviews。有没有办法以编程方式禁用一个 subview View 的自动调整大小? 最佳答案 我假设您确实在使用自动
我正在寻找用 Swift 2.0 编写的适用于 iOS 8.0 或更高版本的答案。我有一个圆形的 UIView,我想扩展它但保留圆形的属性。 要设置 UIView,这是代码。 let frame: C
我有一个有趣的问题,但我不确定如何解决它。我正在尝试使用 @IBInspectable 扩展 UIView。但是,使用这种方法,拐角半径似乎是从 nib 文件的默认端设置的,而不是 View 的实际大
我想将 UIView 移动到圆圈内。 UIView 移动圆内的每个点,但不接触圆的边界线。我正在计算圆和 UIView 之间的距离。 var distance = sqrt( pow((tou
A 是一个自定义的 UIView B是UIImagePickerController.view,这个UIImagePickerController.showsCameraControls = NO A
我无法在启动时修改我的 UIView 高度。 我必须使用 UIView,我希望其中一个屏幕大小 * 70,另一个填补空白。 这是我的 @IBOutlet weak var questionFrame
我一直在想这个问题。 我的情况是我需要在 View 中显示很多网格,我有两个计划。 1、在UIView上画很多矩形。我试过了,但是当我放大 View 时,这条线很模糊。 2、给UIView添加很多CA
我可以通过简单地将容器拖动到新的 UIView 来成功地将第一个 UIView 嵌入到容器中,但是要添加过渡 View ,我无法在按键输入事件上用新 View 替换当前的嵌入 View 。 这是我的代
问题是... 我有一条路径,我创建了一个在其中绘制它的 View 。 View 与路径具有相同的维度。然后,我创建第二个 View ,在其中重写 sizeThatFit: 方法,以便缩放第一个 Vie
我正致力于快速设计自定义 UIimageview。我想使用类似于此的 beizerpath 创建一个 UIimageview 编码应该是快速的。任何帮助表示赞赏。谢谢 最佳答案 创建一个 CAShap
我有一个 iOS 应用程序可以在 UIView 上动态绘制形状(通过 drawRect),我正在研究将该应用程序(或其中的一小部分)移植到 Apple Watch 的可能性。不幸的是,在阅读相关帖子后
我是一名优秀的程序员,十分优秀!