IOS drawrect方法小问题

今天在项目中用drawRect画了一个三角型用于标示section是否折叠,如下图

当折叠的时候会有一个rotation的动画,旋转后三角位置大小都发生了改变

override func drawRect(rect: CGRect) {

// Drawing code

//UIRectFill(self.bounds)

let context = UIGraphicsGetCurrentContext()

CGContextSetRGBStrokeColor(context,253,182,13,1);

CGContextSetLineWidth(context, 0);

CGContextBeginPath(context)

//设置起点

CGContextMoveToPoint(context, 0, 0)

CGContextAddLineToPoint(context, self.frame.size.width, 0)

CGContextAddLineToPoint(context, self.frame.size.width, self.frame.size.width)

//结束路径

CGContextClosePath(context)

CGContextSetFillColorWithColor(context, UIColor.RedColor().CGColor)

CGContextDrawPath(context, CGPathDrawingMode.FillStroke)

}

后来断点发现在cell刷新section的时候重新调用了三角形的drawRect方法,而我在drawRect(rect: CGRect)方法中用了self.frame而不是用他的rect,后来改成rect后显示都正常了,猜测是在调用下面旋转时view的相对坐标发生了转换,不能用view自己的frame,要使用drawRect(rect: CGRect)中的rect

self.triangleView.transform = CGAffineTransformMakeRotation(degree)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容