为 UIView 设置动画并将圆角半径保持为圆形
Animate UIView and keep corner radius as a circle
我有一个圆形的 UIView :
con=[[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width-size-delta, y , size, size)];
con.layer.borderColor=[UIColor whiteColor].CGColor;
con.layer.borderWidth=1.0;
con.backgroundColor=[UIColor clearColor];
con.layer.cornerRadius=con.frame.size.width/2.0;
con.clipsToBounds=YES;
[self addSubview:con];
稍后我想将它的大小更改为更大的,但是当我这样做时,在动画开始时您会看到一些矩形,然后又变回圆形。
我想要一个平面动画,当它的帧发生变化时,它保持为一个圆圈。
CGRect frm=startConfrm; //original frame of the circle
frm.size.width*=2;
frm.size.height*=2;
frm.origin.y=self.frame.size.height/2.0-frm.size.height/2.0;
[UIView animateWithDuration:1.5
delay:0.0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
con.frame=frm;
con.layer.cornerRadius=con.frame.size.width/2.0;
}
completion:^(BOOL finished){ }];
要使视图 "grow" 就位,请不要为框架设置动画。动画转换。
从笔尖加载您的子视图。将其变换设置为 0 的比例:
view.transform = CGAffineTransformMakeScale(1,1);
然后将其添加到您的超级视图中。
在动画块内,将变换设置为恒等式:
view.transform = CGAffineTransformIdentity;
并且视图将增长到正常大小。您可能需要 fiddle 锚点使其从正确的点开始生长。
您也可以更改块内的框架,但如果仅移动视图,我更喜欢更改中心 属性,如果您也有变换,则不应尝试设置框架。
希望对你有帮助!!
EDIT:
Replace your animation block code with the following code:
[UIView animateWithDuration:1.5
delay:0.0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
CGAffineTransform newTransform;
newTransform = CGAffineTransformMakeScale(0, 0);
con.transform = CGAffineTransformScale(newTransform,2,2);
}
completion:^(BOOL finished){ }];
如果您只想缩放视图大小,则可以使用以下代码。
[UIView animateWithDuration:2.0 animations:^{
con.transform = CGAffineTransformMakeScale(2, 2); // it will scale as double size
}completion:^(BOOL finished) {
// do whatever at animation completion
}
}];
要将视图重置为正常大小,
con.transform = CGAffineTransformIdentity;
我有一个圆形的 UIView :
con=[[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width-size-delta, y , size, size)];
con.layer.borderColor=[UIColor whiteColor].CGColor;
con.layer.borderWidth=1.0;
con.backgroundColor=[UIColor clearColor];
con.layer.cornerRadius=con.frame.size.width/2.0;
con.clipsToBounds=YES;
[self addSubview:con];
稍后我想将它的大小更改为更大的,但是当我这样做时,在动画开始时您会看到一些矩形,然后又变回圆形。
我想要一个平面动画,当它的帧发生变化时,它保持为一个圆圈。
CGRect frm=startConfrm; //original frame of the circle
frm.size.width*=2;
frm.size.height*=2;
frm.origin.y=self.frame.size.height/2.0-frm.size.height/2.0;
[UIView animateWithDuration:1.5
delay:0.0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
con.frame=frm;
con.layer.cornerRadius=con.frame.size.width/2.0;
}
completion:^(BOOL finished){ }];
要使视图 "grow" 就位,请不要为框架设置动画。动画转换。
从笔尖加载您的子视图。将其变换设置为 0 的比例:
view.transform = CGAffineTransformMakeScale(1,1);
然后将其添加到您的超级视图中。
在动画块内,将变换设置为恒等式:
view.transform = CGAffineTransformIdentity;
并且视图将增长到正常大小。您可能需要 fiddle 锚点使其从正确的点开始生长。
您也可以更改块内的框架,但如果仅移动视图,我更喜欢更改中心 属性,如果您也有变换,则不应尝试设置框架。
希望对你有帮助!!
EDIT: Replace your animation block code with the following code:
[UIView animateWithDuration:1.5
delay:0.0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
CGAffineTransform newTransform;
newTransform = CGAffineTransformMakeScale(0, 0);
con.transform = CGAffineTransformScale(newTransform,2,2);
}
completion:^(BOOL finished){ }];
如果您只想缩放视图大小,则可以使用以下代码。
[UIView animateWithDuration:2.0 animations:^{
con.transform = CGAffineTransformMakeScale(2, 2); // it will scale as double size
}completion:^(BOOL finished) {
// do whatever at animation completion
}
}];
要将视图重置为正常大小,
con.transform = CGAffineTransformIdentity;