iOS UIView drawRect调整动画大小
我尝试在一个简单的UIView扩展中覆盖drawRect方法,以绘制一个具有圆形边缘的矩形,并用黑色填充它,还可以设置视图动画以在触摸时调整大小。 问题是,调整大小会使图形完全变形,就像上下文在整个时间内保持不变一样。在动画期间从不调用drawRect。。但是,如果我告诉视图在动画之后绘制自身,它将正确绘制 如何在不变形图形的情况下设置调整大小的动画iOS UIView drawRect调整动画大小,ios,uiview,core-animation,drawrect,Ios,Uiview,Core Animation,Drawrect,我尝试在一个简单的UIView扩展中覆盖drawRect方法,以绘制一个具有圆形边缘的矩形,并用黑色填充它,还可以设置视图动画以在触摸时调整大小。 问题是,调整大小会使图形完全变形,就像上下文在整个时间内保持不变一样。在动画期间从不调用drawRect。。但是,如果我告诉视图在动画之后绘制自身,它将正确绘制 如何在不变形图形的情况下设置调整大小的动画 - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCur
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat radius = rect.size.height * 0.5f;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, CGRectGetMidX(rect), CGRectGetMinY(rect));
CGPathAddArcToPoint(path, NULL, CGRectGetMaxX(rect), CGRectGetMinY(rect),
CGRectGetMaxX(rect), CGRectGetMaxY(rect), radius);
CGPathAddArcToPoint(path, NULL, CGRectGetMaxX(rect), CGRectGetMaxY(rect),
CGRectGetMinX(rect), CGRectGetMaxY(rect), radius);
CGPathAddArcToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMaxY(rect),
CGRectGetMinX(rect), CGRectGetMinY(rect), radius);
CGPathAddArcToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMinY(rect),
CGRectGetMaxX(rect), CGRectGetMinY(rect), radius);
CGPathCloseSubpath(path);
CGContextSaveGState(context);
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
CGContextAddPath(context, path);
CGContextFillPath(context);
CGContextRestoreGState(context);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[UIView animateWithDuration:1.f animations:^{
self.frame = CGRectInset(self.frame, 0.f, -100.f);
}];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[UIView animateWithDuration:1.f animations:^{
self.frame = CGRectInset(self.frame, 0.f, +100.f);
}];
}
如果想要UIView的圆角边,则无需显式重新定义drawRect。有一种更简单的方法可以更改UIView的图层特性。在动画期间,将正确绘制视图及其边框
// Make your view background color black
myView.backgroundColor = [UIColor blackColor];
// The following will allow you to change the color/border width/ corner radius of your UIView
myView.layer.borderColor = [UIColor whiteColor].CGColor;
myView.layer.borderWidth = kDefaultBorderWidth;
myView.layer.cornerRadius = kDefaultBorderRadius;
确保已在.m文件中包含
#import
,并且已导入QuartzCore.framework
并确保已设置myView.clipstobunds=YES代码>。是的,但是如果我想在那个圆角矩形上绘制一些自定义效果呢?你想实现什么样的其他效果?我的答案与你的问题有关。。。根据这篇文章,drawrect只适用于一个时间点,所以它不可能适用于动画