美文网首页
IOS drawrect方法小问题

IOS drawrect方法小问题

作者: 杰米 | 来源:发表于2016-03-03 11:46 被阅读954次

今天在项目中用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)

相关文章

网友评论

      本文标题:IOS drawrect方法小问题

      本文链接:https://www.haomeiwen.com/subject/rjsmkttx.html