我试图实现一个动画,当你按住一个按钮时,它会向下动画一个块,当你释放时,它会将它动画回原来的位置,但无论如何我都无法获得动画块的当前位置.这是我的代码:
-(IBAction)moveDown:(id)sender{
CGRect position = [[container.layer presentationLayer] frame];
[movePath moveToPoint:CGPointMake(container.frame.origin.x, position.y)];
[movePath addLineToPoint:CGPointMake(container.frame.origin.x, 310)];
CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
moveAnim.path = movePath.CGPath;
moveAnim.removedOnCompletion = NO;
moveAnim.fillMode = kCAFillModeForwards;
CAAnimationGroup *animGroup = [CAAnimationGroup animation];
animGroup.animations = [NSArray arrayWithObjects:moveAnim, nil];
animGroup.duration = 2.0;
animGroup.removedOnCompletion = NO;
animGroup.fillMode = kCAFillModeForwards;
[container.layer addAnimation:animGroup forKey:nil];
}
-(IBAction)moveUp:(id)sender{
CGRect position = [[container.layer presentationLayer] frame];
UIBezierPath *movePath = [UIBezierPath bezierPath];
[movePath moveToPoint:CGPointMake(container.frame.origin.x, position.y)];
[movePath addLineToPoint:CGPointMake(container.frame.origin.x, 115)];
CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
moveAnim.path = movePath.CGPath;
moveAnim.removedOnCompletion = NO; …Run Code Online (Sandbox Code Playgroud) 在UIViewPropertyAnimator,有没有办法停止所有UIViewPropertyAnimator 动画?
或者也许只是简单地获取所有当前的动画 - 当然,你可以stop全部使用它们.
可以这样做吗?
或者你真的必须(a)只做一个UIViewPropertyAnimator和(b)保持对每个人的参考?
我正在创建一个CAShapeLayer.我正在为它添加旋转动画.不知何故转化结果很奇怪.子图层随着旋转一起移动.我需要它在固定的中心/位置(锚)并旋转.我知道它弄乱了几何变换,但我无法做到正确.
我试过设置anchorpoint.我也跟着这篇文章
这是代码:
UIBezierPath *circle = [UIBezierPath bezierPathWithArcCenter:CGPointMake(75, 125)
radius:50
startAngle:0
endAngle:1.8 * M_PI
clockwise:YES];
CAShapeLayer *circleLayer = [CAShapeLayer layer];
[circleLayer setFrame:CGRectMake(200 - 50, 300 - 50, 100, 100)];
circleLayer.path = circle.CGPath;
circleLayer.strokeColor = [UIColor orangeColor].CGColor;
[circleLayer setFillColor:[UIColor clearColor].CGColor];
circleLayer.lineWidth = 3.0;
if ([circleLayer animationForKey:@"SpinAnimation"] == nil) {
CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
animation.fromValue = [NSNumber numberWithFloat:0.0f];
animation.toValue = [NSNumber numberWithFloat: 2 * M_PI];
animation.duration = 10.0f;
animation.repeatCount = INFINITY;
animation.removedOnCompletion = NO;
[circleLayer addAnimation:animation forKey:@"SpinAnimation"];
}
[self.view.layer addSublayer:circleLayer];
Run Code Online (Sandbox Code Playgroud) 对于一个CALayer,
这是动画,
class Test: CAGradientLayer {
override func draw(in ctx: CGContext) {
super.draw(in: ctx)
startPoint = ....
}
Run Code Online (Sandbox Code Playgroud)
***由于未捕获的异常'CALayerReadOnly'而终止应用程序,原因是:'尝试修改只读层
在draw#inContext调用中似乎无法更改普通的可动画属性之一.
例如:
拥有自己的动画自定义属性简单易行,然后根据它绘制一些东西.这是一些.progress属性的代码,
在动画您的.progress属性时,很容易想象想要设置图层的其他属性,使用基于.progress每个帧的值的一些公式.
但是,你不能在draw#in功能中做到这一点 - 怎么做?
我正在尝试为UIButton制作动画.但在动画期间,没有与UIButton的互动.预期的行为是能够在移动时单击按钮.这是UIButton和动画的代码片段:
UIImage *cloudImage = [UIImage imageNamed:@"sprite.png"];
UIButton moveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[moveBtn setFrame:CGRectMake(0.0, 80.0, cloudImage.size.width, cloudImage.size.height)];
[moveBtn setImage:cloudImage forState:UIControlStateNormal];
[moveBtn addTarget:self action:@selector(hit:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:moveBtn];
CGPoint newLeftCenter = CGPointMake( 300.0f + moveBtn.frame.size.width / 2.0f, moveBtn.center.y);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:5.0f];
[UIView setAnimationRepeatCount:HUGE_VALF];
moveBtn.center = newLeftCenter;
[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)
hit选择器只显示一个NSLog,以显示按钮是否响应它.任何帮助,将不胜感激.
REPRO存储库在这里:https://github.com/kaolin/38492498/tree/master
我有一个@property int percentScanned.
我设置了needsDisplayForKey:
+ (BOOL)needsDisplayForKey:(NSString *)key {
if ([@"percentScanned" isEqualToString:key]) {
return YES;
}
return [super needsDisplayForKey:key];
}
Run Code Online (Sandbox Code Playgroud)
我的显示方法:
- (void)display {
_percentTextLayer.string = [NSString stringWithFormat:@"%02d%%",[[self presentationLayer] percentScanned]];
}
Run Code Online (Sandbox Code Playgroud)
我的动画:
[CATransaction begin];
self.percentScanned = 99;
_percentTextLayer.string=@"99%";
[CATransaction setCompletionBlock:^{
_percentTextLayer.string=@"99%";
self.percentScanned = 99;
}];
{
CABasicAnimation *a = [CABasicAnimation animationWithKeyPath:@"percentScanned"];
[a setFromValue:@0];
[a setToValue:@99];
[a setDuration:4.];
[self addAnimation:a forKey:@"percentage"];
}
[CATransaction commit];
Run Code Online (Sandbox Code Playgroud)
这一切都很好,除了在动画结束时,我的模型层字符串返回到"00%".正如你所看到的,我已经抛出,如厨房水槽迫使它留在"99%"什么感觉(外的"黑客"的animation.removedOnCompletion = NO;和animation.fillMode = kCAFillModeForwards;-and实际上,添加这些并不能帮助!).
我做有关于一秒钟后3/30在同一层上另一个动画,淡入淡出的不透明度....但推说出来,或者删除它,似乎不有所作为. …
之前已经问过这个问题但是方式略有不同,我无法按照我想要的方式得到任何答案,所以我希望有很棒的核心动画技能可以帮助我.
我桌子上有一套卡片.当用户向上或向下滑动时,该组卡片在桌子上上下移动.在任何给定时间屏幕上都可以看到4张卡片,但只有第二张卡片显示其面部.当用户滑动时,第二张卡片翻转回到其脸部,并且下一张卡片(取决于滑动方向)落在其显示其脸部的位置.
我已经设置了我的卡片视图类:
@interface WLCard : UIView {
UIView *_frontView;
UIView *_backView;
BOOL flipped;
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用这段代码翻转卡片:
- (void) flipCard {
[self.flipTimer invalidate];
if (flipped){
return;
}
id animationsBlock = ^{
self.backView.alpha = 1.0f;
self.frontView.alpha = 0.0f;
[self bringSubviewToFront:self.frontView];
flipped = YES;
CALayer *layer = self.layer;
CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
rotationAndPerspectiveTransform.m34 = 1.0 / 500;
rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, M_PI, 1.0f, 0.0f, 0.0f);
layer.transform = rotationAndPerspectiveTransform;
};
[UIView animateWithDuration:0.25
delay:0.0
options: UIViewAnimationCurveEaseInOut
animations:animationsBlock
completion:nil];
}
Run Code Online (Sandbox Code Playgroud)
这段代码有效,但它有以下问题我似乎无法弄清楚:
另外,请记住,我需要能够与卡的面部进行交互.即它上面有按钮.
如果有人遇到这些问题,那么看到你的解决方案会很棒.更好的方法是为动画添加一个透视变换,使其具有额外的真实感.
我正在制作一款适用于Mac的纸牌游戏,而我正在使用CABasicAnimation来使卡片翻转.它几乎可以工作,但可能会更好一些.
当它现在工作时,卡片向内翻转(向左) - 屏幕截图1.当卡片"向上翻转"一直向左移动时,我更改了NSView的图像并再次将卡片向外翻转 - 屏幕截图2.
截图1(翻转):

截图2(翻出):

翻阅代码:
- (void)flipAnimationInwards{
// Animate shadow
NSShadow *dropShadow = [[NSShadow alloc] init];
[dropShadow setShadowOffset:NSMakeSize(0, 1)];
[dropShadow setShadowBlurRadius:15];
[dropShadow setShadowColor:[NSColor colorWithCalibratedWhite:0.0 alpha:0.5]];
[[self animator] setShadow:dropShadow];
// Create CAAnimation
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
rotationAnimation.fromValue = [NSNumber numberWithFloat: 0.0];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI/2];
rotationAnimation.duration = 3.1;
rotationAnimation.repeatCount = 1.0;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
rotationAnimation.fillMode = kCAFillModeForwards;
rotationAnimation.removedOnCompletion = NO;
[rotationAnimation setValue:@"flipAnimationInwards" forKey:@"flip"];
rotationAnimation.delegate = self;
// Get the layer
CALayer* lr = …Run Code Online (Sandbox Code Playgroud) 我设法沿着UIBezierPath为CALayer制作动画.
我想要完成的是仅为路径的一定百分比设置动画,例如,只有路径的25%,并且该层保持在该位置(25%).
这样做的方法是什么?这是我的代码,它总是为完整路径设置动画.
let aPath = UIBizierPath(CGPath: somePath)
let anim = CAKeyframeAnimation(keyPath: "position")
anim.path = aPath.CGPath
anim.rotationMode = kCAAnimationRotateAuto
anim.repeatCount = 1
anim.fillMode = kCAFillModeForwards
anim.removedOnCompletion = false
anim.duration = 3.0
anim.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
ticker.addAnimation(anim, forKey: "animate_ticker")
Run Code Online (Sandbox Code Playgroud) 理想情况下我需要类似的东西UIView.animateWithDuration,但我知道这不是macOS上可用的API.操作NSView的实际帧并为更改设置动画的最佳方法是什么?
我尝试过CABasicAnimation,但是这是在CALayer而不是视图本身上执行的.
我需要的是为实际的视图框架设置动画......(或者这可能是一种不好的做法,还有另一种选择吗?)
因此,经过一番挖掘,我发现了NSAnimationContext.然而,这似乎只是在两个状态之间插入/淡化,这不是我想要的感觉.
这是我的代码的一小部分示例:
override func mouseDown(with event: NSEvent) {
let newSize = NSMakeRect(self.frame.origin.x,
self.frame.origin.y - 100,
self.frame.width, self.frame.height + 100)
NSAnimationContext.runAnimationGroup({ (context) -> Void in
context.duration = 0.5
self.animator().frame = newSize
}, completionHandler: {
Swift.print("completed")
})
}
Run Code Online (Sandbox Code Playgroud) caanimation ×10
ios ×7
calayer ×4
objective-c ×3
cgrect ×1
cocoa ×1
cocoa-touch ×1
iphone ×1
macos ×1
swift ×1
uibutton ×1