我试图动画的变化cornerRadius一个的UIView实例layer,但的变化cornerRadius立即生效.
这是代码:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
view.layer.masksToBounds = YES;
view.layer.cornerRadius = 10.0;
[UIView animateWithDuration:1.0 animations:^{
[view.layer.cornerRadius = 0.0;
}];
Run Code Online (Sandbox Code Playgroud)
谢谢大家给我任何提示.
编辑:我设法Core Animation使用a 来动画这个属性CABasicAnimation.
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
animation.fromValue = [NSNumber numberWithFloat:10.0f];
animation.toValue = [NSNumber numberWithFloat:0.0f];
animation.duration = 1.0;
[viewToAnimate.layer addAnimation:animation forKey:@"cornerRadius"];
[animation.layer setCornerRadius:0.0];
Run Code Online (Sandbox Code Playgroud) 如何将矩形图像视图转换为可以在自动布局中保持形状而不设置宽度和高度限制的圆形图像视图?从而允许imageView定义它的大小,并且相对于其周围的对象,其大小越来越小,具有前导,尾随,顶部和底部约束.
前几天我问了一个类似的问题,但我认为这可能会以更简洁的方式提出.非常感谢!
编辑
好的,我开始尝试尽可能简单.我有一个名为"Cell"的视图和一个名为"dog"的UIImageView,就是这样.我不再在控制台中"无法同时满足约束",只是使用自动布局的两个简单视图.我仍在尝试使用此代码来绕过UIImageView:
profileImageView.layer.cornerRadius = profileImageView.frame.size.width / 2
profileImageView.clipsToBounds = true
Run Code Online (Sandbox Code Playgroud)
这是单元格约束设置:
这是配置文件pic约束设置:
这是没有代码的结果,没有舍入,但是很好和正方形:
以下是圆形代码的结果:
这对我来说没有意义,因为没有舍入代码,图像是方形的,并且代码是菱形的.如果它是正方形不应该是一个没有问题的圆圈?
编辑2
这是当我删除底部约束并为superview添加相同高度的.637乘数时发生的情况.
我正在尝试创建一个UIView,它显示一个半透明的圆圈,其边界内有一个不透明的边框.我希望能够以两种方式改变界限 - 在一个-[UIView animateWithDuration:animations:]块内和一个捏动手势识别器动作,每秒触发几次.我已经尝试了三种基于SO其他地方的答案的方法,但没有一种方法是合适的.
设置视图图层的角半径可以layoutSubviews提供平滑的平移,但在动画期间视图不会保持圆形; 似乎cornerRadius不可动画.
绘制圆圈可以drawRect:得到一致的圆形视图,但如果圆圈太大,那么在捏合手势中调整大小会变得不稳定,因为设备花费了太多时间重绘圆圈.
添加CAShapeLayer并设置其路径属性layoutSublayersOfLayer,该属性不会在UIView动画内部制作动画,因为路径不是隐式可动画的.
有没有办法让我创建一个始终循环且平滑可调整大小的视图?我可以使用其他类型的层来利用硬件加速吗?
UPDATE
当我说我想改变一个-[UIView animateWithDuration:animations:]区块内的界限时,一位评论者要求我扩展我的意思.在我的代码中,我有一个包含我的圆视图的视图.圆形视图(使用cornerRadius的版本)覆盖-[setBounds:]以设置角半径:
-(void)setBounds:(CGRect)bounds
{
self.layer.cornerRadius = fminf(bounds.size.width, bounds.size.height) / 2.0;
[super setBounds:bounds];
}
Run Code Online (Sandbox Code Playgroud)
圆视图的边界设置为-[layoutSubviews]:
-(void)layoutSubviews
{
// some other layout is performed and circleRadius and circleCenter are
// calculated based on the properties and current size of the view.
self.circleView.bounds = CGRectMake(0, 0, circleRadius*2, circleRadius*2);
self.circleView.center = circleCenter;
}
Run Code Online (Sandbox Code Playgroud)
视图有时会在动画中调整大小,如下所示:
[UIView animateWithDuration:0.33 animations:^(void) {
myView.frame = …Run Code Online (Sandbox Code Playgroud) 在我正在研究的CALayer子类中,我有一个我想自动动画的自定义属性,也就是说,假设该属性被称为"myProperty",我想要以下代码:
[myLayer setMyProperty:newValue];
Run Code Online (Sandbox Code Playgroud)
使得当前值的平滑动画变为"newValue".
使用覆盖actionForKey:和needsDisplayForKey的方法:(参见下面的代码)我能够非常好地运行它来简单地在旧值和新值之间进行插值.
我的问题是我想使用稍微不同的动画持续时间或路径(或其他),具体取决于属性的当前值和新值,我无法弄清楚如何从actionForKey中获取新值:
提前致谢
@interface ERAnimatablePropertyLayer : CALayer {
float myProperty;
}
@property (nonatomic, assign) float myProperty;
@end
@implementation ERAnimatablePropertyLayer
@dynamic myProperty;
- (void)drawInContext:(CGContextRef)ctx {
... some custom drawing code based on "myProperty"
}
- (id <CAAction>)actionForKey:(NSString *)key {
if ([key isEqualToString:@"myProperty"]) {
CABasicAnimation *theAnimation = [CABasicAnimation animationWithKeyPath:key];
theAnimation.fromValue = [[self presentationLayer] valueForKey:key];
... I want to do something special here, depending on both from and to values...
return theAnimation;
}
return [super actionForKey:key];
}
+ (BOOL)needsDisplayForKey:(NSString …Run Code Online (Sandbox Code Playgroud)