IOS学习:UIView小记

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"]]];



     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值