1、drawRect方法中的画图
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
// 清空屏幕
CGContextClearRect(context, rect);
// 画底图
[self.imagedrawInRect:rect];
// 竖直方向
CGRect rectt = CGRectMake(self.touchPoint.x - 5, 0, 10, self.frame.size.height
);
// 水平方向
CGRect rectt2 = CGRectMake(0, self.touchPoint.y - 5, self.frame.size.width, 10);
CGContextSetRGBFillColor(context, 1.0f, 0.0f, 0.0f, 0.3);
CGContextFillRect(context, rectt);
CGContextFillRect(context, rectt2);
}
2、touchBegan方法中用NSSet参数取当前触摸点
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
self.touchPoint = [touch locationInView:self];
NSLog(@"[%f, %f]", self.touchPoint.x, self.touchPoint.y);
[selfsetNeedsDisplay];
}
3、注意
不要手动调用drawRect方法,当想要重绘view时,应该调用UIView的setNeedDisplay方法
4、设置UIView的背影图片
[self.viewsetBackgroundColor:[UIColorcolorWithPatternImage:[UIImageimageNamed:@"main_bg.jpg"]]];