- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我有一个 CAShapeLayer(它是 UIView 子类的层),其 path
应该在 View 的 bounds
时更新尺寸变化。为此,我覆盖了图层的 setBounds:
重置路径的方法:
@interface CustomShapeLayer : CAShapeLayer
@end
@implementation CustomShapeLayer
- (void)setBounds:(CGRect)bounds
{
[super setBounds:bounds];
self.path = [[self shapeForBounds:bounds] CGPath];
}
path
属性默认不动画,我想出了这个:
addAnimation:forKey:
方法。在其中,确定是否向图层添加了边界动画。如果是这样,通过复制传递给该方法的边界动画中的所有属性,创建第二个显式动画来为边界旁边的路径设置动画。在代码中:
- (void)addAnimation:(CAAnimation *)animation forKey:(NSString *)key
{
[super addAnimation:animation forKey:key];
if ([animation isKindOfClass:[CABasicAnimation class]]) {
CABasicAnimation *basicAnimation = (CABasicAnimation *)animation;
if ([basicAnimation.keyPath isEqualToString:@"bounds.size"]) {
CABasicAnimation *pathAnimation = [basicAnimation copy];
pathAnimation.keyPath = @"path";
// The path property has not been updated to the new value yet
pathAnimation.fromValue = (id)self.path;
// Compute the new value for path
pathAnimation.toValue = (id)[[self shapeForBounds:self.bounds] CGPath];
[self addAnimation:pathAnimation forKey:@"path"];
}
}
}
keyPath
对
@"bounds"
而不是 `@"bounds.size"。)
[UIView animateWithDuration:1.0 delay:0.0 usingSpringWithDamping:0.3 initialSpringVelocity:0.0 options:0 animations:^{
self.customShapeView.bounds = newBounds;
} completion:nil];
EXC_BAD_ACCESS
中 ( CGPathApply()
) 崩溃在上一个动画仍在进行中时触发此动画。我不确定这是否与我的特定实现有关。编辑:没关系。我忘了转换 UIBezierPath
至 CGPathRef
. Thanks @antonioviero ! actionForKey:
或 View 的 actionForLayer:forKey:
为了返回 path
的自定义动画对象属性(property)。我认为这将是首选方式,但我没有找到获取应由封闭动画块设置的事务属性的方法。即使从动画块内部调用,[CATransaction animationDuration
] 等总是返回默认值。 actionForLayer:forKey:
或图层的 actionForKey:
方法并检查传递给此方法的键是否是您要设置动画的键( @"path"
)。 super
在图层的“正常”动画属性之一(例如 @"bounds"
)上,将隐含地告诉您您是否在动画块内。如果 View (层的委托(delegate))返回一个有效的对象(而不是 nil
或 NSNull
),我们是。 [super actionForKey:]
返回的动画中设置路径动画的参数(持续时间、计时函数等)并返回路径动画。 actionForLayer:forKey:
返回的。不是标准(并记录在案)
CA...Animation
但是一个私有(private)实例
_UIViewAdditiveAnimationAction
类(
NSObject
子类)。由于没有记录此类的属性,因此我无法轻松使用它们来创建路径动画。
backgroundColor
仍然返回标准
CA...Animation
在 iOS 8 上。我必须尝试一下。`
最佳答案
我知道这是一个老问题,但我可以为您提供一个类似于洛克伍德先生方法的解决方案。遗憾的是,这里的源代码很快,因此您需要将其转换为 ObjC。
如前所述,如果该层是 View 的支持层,您可以拦截 View 本身中的 CAAction。然而,这并不方便,例如,如果在多个 View 中使用背衬层。
好消息是 actionForLayer:forKey: 实际上在支持层调用 actionForKey: 。
它位于 actionForKey: 在支持层中,我们可以在其中拦截这些调用并在路径更改时提供动画。
用 swift 编写的示例层如下:
class AnimatedBackingLayer: CAShapeLayer
{
override var bounds: CGRect
{
didSet
{
if !CGRectIsEmpty(bounds)
{
path = UIBezierPath(roundedRect: CGRectInset(bounds, 10, 10), cornerRadius: 5).CGPath
}
}
}
override func actionForKey(event: String) -> CAAction?
{
if event == "path"
{
if let action = super.actionForKey("backgroundColor") as? CABasicAnimation
{
let animation = CABasicAnimation(keyPath: event)
animation.fromValue = path
// Copy values from existing action
animation.autoreverses = action.autoreverses
animation.beginTime = action.beginTime
animation.delegate = action.delegate
animation.duration = action.duration
animation.fillMode = action.fillMode
animation.repeatCount = action.repeatCount
animation.repeatDuration = action.repeatDuration
animation.speed = action.speed
animation.timingFunction = action.timingFunction
animation.timeOffset = action.timeOffset
return animation
}
}
return super.actionForKey(event)
}
}
关于ios - 动画边界变化时的 CAShapeLayer 路径动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24936987/
我编写了一个应用程序,它有一个 UIViewController,它在纵向模式下显示另一个 UIViewController,在横向模式下显示不同的 UIViewController。 当我去风景时,
我想为 UISegmentedControl 提供以下方面: 注意灰色背景 View ,以及分段控件未选定项目的白色背景。 但是,如果我为 UISegmentedControl 提供白色背景,我会得到
我正在尝试为我的可排序项目创建边界。我看过这个问题/答案: jquery sortable keep within container Boundary 并尝试将我的 JS 以此为基础,但无论出于何种
我正在尝试编写执行以下操作的代码:如果我单击起始位置为 (100,100) 的字符串 C(JLabel),该字符串将在 JFrame 的边界内移动。代码本身并不难实现,但我遇到了问题为 JLabel
我有一个 .xib 文件,其中包含我想用来播放视频文件的 View 。该 View 具有配置其大小和位置的约束。现在我需要获取这些来配置我的视频播放器: let slide1: OnboardingS
我将从 Google map 转到 Apple map 。 Google map 能够根据东北和西南坐标更新相机,如下所示: let bounds = GMSCameraUpdate.fit(GMSC
这个问题在这里已经有了答案: Border over a bitmap with rounded corners in Android (6 个答案) 关闭 6 年前。 如何为我的图片添加圆角边框?
我有一个任务是使用java.awt.Graphics绘制一定数量的圆圈。 绘制圆圈相当简单,但我只应该在圆圈出现在可见区域内时绘制圆圈。我知道我可以调用方法 getClipBounds() 来确定绘图
我在设置过渡时遇到问题,目前它是从上到下(它是悬停时显示的边框)。我希望过渡从中间开始并传播到侧面,或者至少从任何一侧开始并传播到另一侧... 我的导航菜单 anchor 使用导航链接类! * {
我来自 Java,目前正在学习 C++。我正在使用 Stroustrup 的 Progamming Principles and Practice of Using C++。我现在正在使用 vecto
我有一个要展开的循环: for(int i = 0; i < N; i++) do_stuff_for(i); 展开: for(int i = 0; i < N; i += CHUNK) {
Scala 中是否有类似 View 绑定(bind)但可以匹配子类型的东西? 由于 Scala 中的 View 没有链接,我目前有以下内容: implicit def pimpIterable[A,
网站用户输入地址。 如果地址在边界内,则“合格”。如果地址超出边界,则“不合格”。 是否有现有的小部件或代码可以执行此操作?有人知道实现这一目标的第一步吗?感谢您的任何意见。 最佳答案 哇,反对票是怎
我有以下测试应用程序: import Codec.Crypto.AES import qualified Data.ByteString.Char8 as B key = B.pack "Thisis
我正在尝试添加一个 JButton,但它与进度条水平对齐。如何将 JButton 对齐到下面的线上? 另外,我试图将所有组件分组到不同的组中,但我不确定如何执行此操作。有谁知道吗? 最佳答案 要简单分
假设我们有一个像上面这样的相框。从中心开始,如何找到可用于绘制的面积最大的矩形(矩形中的所有像素必须为 rgb(255,255,255)? 我需要找到图中所示的A点和B点的x和y坐标。 我的方法之一是
这可能是一个愚蠢的问题,但当我创建一个类时,我应该如何正确设置其中属性的边界。 例子:如果我有这门课 class Product { private string name; publ
我正在从 leaflet 迁移回来,如果我需要 map 绑定(bind),我使用以下代码: var b = map.getBounds(); $scope.filtromapa.lat1 = b.ge
我正在学习如何创建自定义 UIView。我正在制作的这个特定 View 包含几个按钮。我注意到,当我从惰性实例化 block 中调用frame/height属性时,我得到的值是128,但是当我调用dr
我正在尝试制作一个弹跳球。设置的边界允许球在超出框架边界后从起点开始。我无法让球弹起来。一旦击中边界(框架的外边缘),如何让球弹起?我相信问题出在 moveBall() 方法中。 主类 导入 java
我是一名优秀的程序员,十分优秀!