我的-drawInContext:方法中有一个包含一些复杂绘图代码的图层.我正在尝试最小化我需要做的绘图量,所以我使用-setNeedsDisplayInRect:仅更新已更改的部分.这是非常好的工作.但是,当图形系统更新我的图层时,它会使用交叉淡入淡出从旧图像过渡到新图像.我希望它能立即切换.
我已经尝试使用CATransaction来关闭操作并将持续时间设置为零,并且都不起作用.这是我正在使用的代码:
[CATransaction begin];
[CATransaction setDisableActions: YES];
[self setNeedsDisplayInRect: rect];
[CATransaction commit];
Run Code Online (Sandbox Code Playgroud)
我应该使用不同的CATransaction方法(我也尝试过-setValue:forKey:使用kCATransactionDisableActions,结果相同).
我掩盖了这样的图像:
UIView *maskImage; maskImage = [[UIView alloc] init];
maskImage.backgroundColor = UIColorFromRGB(FTRMaskColor);
maskImage.frame = newFrame;
CALayer *theLayer = [CALayer layer];
theLayer.contents = (id)[[UIImage imageNamed:@"image.png"] CGImage];
theLayer.frame = newFrame;
maskImage.layer.mask = theLayer;
Run Code Online (Sandbox Code Playgroud)
它工作正常但主要的问题是如果我想旋转我的Ipad,视图的旋转动画或图层(我不是很确定)不起作用.它旋转没有动画.你能帮忙吗?
我对CABasicAnimation有一些问题.它类似于那篇文章:CABasicAnimation旋转返回到原始位置
所以,我有在touchMove中旋转的uiimageview.在touchEnd调用方法做"惯性动画":
-(void)animationRotation: (float)beginValue
{
CABasicAnimation *anim;
anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
anim.duration = 0.5;
anim.repeatCount = 1;
anim.fillMode = kCAFillModeForwards;
anim.fromValue = [NSNumber numberWithFloat:beginValue];
[anim setDelegate:self];
anim.toValue = [NSNumber numberWithFloat:(360*M_PI/180 + beginValue)];
[appleView.layer addAnimation:anim forKey:@"transform"];
CGAffineTransform rot = CGAffineTransformMakeRotation(360*M_PI/180 + beginValue);
appleView.transform = rot;
}
Run Code Online (Sandbox Code Playgroud)
这个动画工作正常,但如果我在animationRotation结束前调用touchBegan,则旋转角度为beginValue.我需要阴极电流旋转角度.作为一个实验,我宣布方法
-(vod) animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
NSLog(@"Animation finished!");
}
Run Code Online (Sandbox Code Playgroud)
它看起来很有效.但我不知道如何在animationDidStop中为我的UIImageView获取角度值或CGAffineTransform.甚至可以做到吗?谢谢.
我正在使用创建的旋转动画CABasicAnimation.它旋转UIView超过2秒.但我需要能够在UIView触摸时停止它.如果我删除动画,则视图与动画开始前的位置相同.
这是我的动画代码:
float duration = 2.0;
float rotationAngle = rotationDirection * ang * speed * duration;
//rotationAngle = 3*(2*M_PI);//(double)rotationAngle % (double)(2*M_PI) ;
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: rotationAngle ];
rotationAnimation.duration = duration;
rotationAnimation.cumulative = YES;
rotationAnimation.removedOnCompletion = NO;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
rotationAnimation.fillMode = kCAFillModeForwards;
rotationAnimation.delegate = self;
[self.view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
Run Code Online (Sandbox Code Playgroud)
如何在UIView旋转时将旋转停在哪里?我知道如何管理触摸部分,但我无法弄清楚如何在动画的当前角度停止视图.
解决方案: 我通过获取表示层的角度,移除动画并设置视图的变换来解决问题.这是代码:
[self.view.layer removeAllAnimations];
CALayer* presentLayer = self.view.layer.presentationLayer;
float currentAngle = [(NSNumber *)[presentLayer valueForKeyPath:@"transform.rotation.z"] floatValue]; …Run Code Online (Sandbox Code Playgroud) 我试图将旋转动画按度数应用于a UIImageView并在完成块中保持旋转变换.
我面临的问题是,当执行完成块时,通过从动画的结束状态传递到完成块产生可见的闪烁.
这是我目前使用的代码:
if (futureAngle == currentAngle) {
return;
}
float rotationAngle;
if (futureAngle < currentAngle) {
rotationAngle = futureAngle - currentAngle;
}else{
rotationAngle = futureAngle - currentAngle;
}
float animationDuration = fabs(rotationAngle) / 100;
rotationAngle = GLKMathDegreesToRadians(rotationAngle);
[CATransaction begin];
CABasicAnimation *rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.byValue = [NSNumber numberWithFloat:rotationAngle];
rotationAnimation.duration = animationDuration;
rotationAnimation.removedOnCompletion = YES;
[CATransaction setCompletionBlock:^{
view.transform = CGAffineTransformRotate(view.transform, rotationAngle);
}];
[view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
[CATransaction commit];
Run Code Online (Sandbox Code Playgroud) 不要引用CABasicAnimation 在下一个动画和Objective-C之前返回到原始位置- CABasicAnimation 在动画之后应用更改?和CABasicAnimation 旋转返回到 我尝试过的原始位置。
below code does,bottom->top->goes left->back to it’s original position.
bottom->top->goes left->back to its original position.
I need bottom->top->goes left.bottom->top->goes left so on…
-(void)addUpDownAnimationForButton:(UILabel*)label
{
CABasicAnimation * bottomAnimation ;
bottomAnimation =[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
[bottomAnimation setValue:@"animation1" forKey:@"id"];
bottomAnimation.delegate = self;
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:)];
bottomAnimation.duration = 2.0;
bottomAnimation.fromValue = [NSNumber numberWithFloat:13];
bottomAnimation.toValue = [NSNumber numberWithFloat:-7];
bottomAnimation.repeatCount = 0;
bottomAnimation.fillMode = kCAFillModeForwards;
bottomAnimation.removedOnCompletion = NO;
[btnSpecialForListing.titleLabel.layer addAnimation:bottomAnimation forKey:@"transform.translation.y"];
}
- (void)animationDidStop:(CAAnimation *)theAnimation2 …Run Code Online (Sandbox Code Playgroud) ios ×6
objective-c ×4
iphone ×3
calayer ×2
animation ×1
caanimation ×1
ipad ×1
rotation ×1
xcode4 ×1