在我的iOS 5应用程序中,我有一个自定义UIButton的红色球体作为其图像.当按下按钮时,我希望orb开始在红色和绿色之间发出脉冲/褪色.我有红色和绿色图像,可以使用以下代码在两者之间成功交叉解析:
CABasicAnimation *imageSwitchAnimation = [CABasicAnimation animationWithKeyPath:@"contents"];
imageSwitchAnimation.fromValue = (id)[UIImage imageNamed:@"red.png"].CGImage;
imageSwitchAnimation.toValue = (id)[UIImage imageNamed:@"green.png"].CGImage;
imageSwitchAnimation.duration = 1.5f;
[self.button.imageView.layer addAnimation:imageSwitchAnimation forKey:@"animateContents"];
Run Code Online (Sandbox Code Playgroud)
但是,我希望动画能够永久地继续(好吧,直到我告诉它停止)以及动画反转和循环.换句话说,淡红色 - >绿色 - >红色然后重复.
我尝试将上面的动画块放到无限循环中(连同一些逻辑来确定渐变是否应该从红色 - >绿色或绿色 - >红色)但这只是锁定整个应用程序.
其他解决方案似乎使用Cocos2d,这看起来相当重,因为这是我在应用程序中需要的唯一动画(所以除非绝对需要,否则我不想使用这样的框架).
任何帮助将不胜感激.
我目前正在使用以下动画UITableViewCell:
CATransform3D rotationTransform = CATransform3DMakeRotation(1.0f * M_PI, 0, 0, 1.0);
CABasicAnimation* rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
rotationAnimation.toValue = [NSValue valueWithCATransform3D:rotationTransform];
rotationAnimation.duration = 0.25f;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 1;
[cell.rotatingImage.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
Run Code Online (Sandbox Code Playgroud)
然而,当如上所述动画~3个单元时,动画变得非常迟钝.有什么办法可以减少这种滞后吗?
在我的应用程序中,我使用CABasicAnimation进行动画制作.我想动态更改动画的速度,所以我添加了一个滑块来改变速度.以下是我的动画代码.但是我无法改变速度,当我改变速度值时没有任何反应.
CABasicAnimation * a = [CABasicAnimation animationWithKeyPath:@"position"];
[a setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
CGPoint startPt = CGPointMake(self.view.bounds.size.width + displayLabel.bounds.size.width / 2,
displayLabel.frame.origin.y);
CGPoint endPt = CGPointMake(displayLabel.bounds.size.width / -2, displayLabel.frame.origin.y);
[a setFromValue:[NSValue valueWithCGPoint:startPt]];
[a setToValue:[NSValue valueWithCGPoint:endPt]];
[a setAutoreverses:NO];
[a setDuration:speeds];
[a setRepeatCount:HUGE_VAL];
[displayLabel.layer addAnimation:a forKey:@"rotationAnimation"];
- (IBAction)speedSlider:(id)sender {
speeds = slider.value;
}
Run Code Online (Sandbox Code Playgroud) 我正在绘制一个基本圆圈的动画.这很好用,除了动画开始在3点位置绘制.有谁知道我怎么能在12点开始?
self.circle = [CAShapeLayer layer];
self.circle.fillColor = nil;
self.circle.lineWidth = 7;
self.circle.strokeColor = [UIColor blackColor].CGColor;
self.circle.bounds = CGRectMake(0, 0, 200, 200);
self.circle.path = [UIBezierPath bezierPathWithOvalInRect:self.circle.bounds].CGPath;
[self.view.layer addSublayer:self.circle];
CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
drawAnimation.duration = 5.0;
drawAnimation.repeatCount = 1.0;
drawAnimation.removedOnCompletion = NO;
drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
drawAnimation.toValue = [NSNumber numberWithFloat:1.0f];
drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[self.circle addAnimation:drawAnimation forKey:@"drawCircleAnimation"];
Run Code Online (Sandbox Code Playgroud) 我已经实现了一个LoadingLayer类,它有一些动画元素(准确地说是2个),它们是动画的CABasicAnimation.现在,此加载屏幕视图在整个应用程序中持续存在.隐藏视图是否会停止所有动画?或者让我重新说一句:是否有很多记忆我不得不担心或可能导致滞后?我尝试使用-pauseLayer:和-resumeLayer:方法,但由于我的应用程序中的多线程,它们导致了一些问题.因此,只需隐藏并显示加载屏幕,其动画一直在运行吗?
我需要设置zoomScale回到1上一个UIScrollView,但我需要它来进行动画.我以为我可以使用CABasicAnimation这一点,但NSValue似乎并不具有"valueForFloat"或"valueForNSNumber",所以我不知道用什么animation.fromValue和animation.toValue为CABasicAnimation.有没有更好的方法来解决这个问题?
编辑:我需要添加什么才能工作?
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"zoomScale"];
animation.duration = 2.3;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
animation.fromValue = [NSNumber numberWithFloat:myScrollview.zoomScale];
animation.toValue = [NSNumber numberWithFloat:1.0];
[myScrollview.layer addAnimation:animation forKey:@"zoomScale"];
Run Code Online (Sandbox Code Playgroud) 我有一个以编程方式调用的UIImageView,我试图让它旋转但它不起作用.图像将被放置在动态UITableView中(我无法将其更改为静态).我的表视图中的图像看起来不错,但它不会旋转.
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 122, 38, 38)];
imageView.image = [UIImage imageNamed:@"settings1.png"];
[self.view addSubview:imageView];
CABasicAnimation *spin;
spin = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
spin.fromValue = [NSNumber numberWithFloat:0];
spin.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
spin.duration = 4;
spin.repeatCount = 10*1000;
[imageView.layer addAnimation:spin forKey:@"360"];
Run Code Online (Sandbox Code Playgroud) 有人可以请给我一个动画图像视图的不透明度的示例吗?
我甚至无法在目标c中找到一个好的例子
func showCorrectImage(element: AnyObject){
var animation : CABasicAnimation = CABasicAnimation(keyPath: "opacity");
animation.delegate = self
animation.fromValue = NSValue(nonretainedObject: 0.0)
animation.toValue = NSValue(nonretainedObject: 1.0)
animation.duration = 1.0
element.layer?.addAnimation(animation, forKey: nil)
}
Run Code Online (Sandbox Code Playgroud)
我认为我有大部分权利(尽管不完全确定),有人可以帮助我吗?
element =图像视图
提前谢谢!〜
出于某种原因,这对我不起作用:
let color = CABasicAnimation(keyPath: "borderColor")
color.fromValue = sender.layer.borderColor;
color.toValue = UIColor.redColor().CGColor;
color.duration = 2;
color.repeatCount = 1;
sender.layer.addAnimation(color, forKey: "color and width");
Run Code Online (Sandbox Code Playgroud)
我没有得到任何动画.
我正在尝试将动画添加到UITextField用户编辑的边框.
想法是在编辑第一个文本字段后在登录页面中显示线条动画,然后在用户切换到下一个文本字段后,该行应移动到下面的文本字段.
我做了一个不能像我期待的那样努力的尝试.
我的代码:
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var verticSpace: NSLayoutConstraint!
@IBOutlet weak var usernameTxtField: UITextField!
@IBOutlet weak var passwordTxtField: UITextField!
weak var shapeLayer: CAShapeLayer?
let path = UIBezierPath()
let shapeLayerNew = CAShapeLayer()
override func viewDidLoad() {
super.viewDidLoad()
usernameTxtField.delegate = self
passwordTxtField.delegate = self
}
func textFieldDidBeginEditing(_ textField: UITextField) {
let path = UIBezierPath()
if textField == usernameTxtField {
if textField.text == "" {
self.startMyLine()
}
}
if passwordTxtField.isFirstResponder {
let path2 = UIBezierPath()
path2.move(to: …Run Code Online (Sandbox Code Playgroud) cabasicanimation ×10
ios ×7
objective-c ×4
swift ×3
cocoa-touch ×2
nsvalue ×2
uiimageview ×2
cashapelayer ×1
ios5 ×1
ipad ×1
iphone ×1
uibezierpath ×1
uiscrollview ×1
uitableview ×1
uitextfield ×1
xcode ×1