老师为什么我运行后画不出红线

老师为什么我运行后画不出红线

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//
//  AppDelegate.h
//  锁屏
//
//  Created by  on 2018/10/29.
//  Copyright © 2018 lzn. All rights reserved.
//
 
#import <UIKit/UIKit.h>
 
@interface AppDelegate : UIResponder <UIApplicationDelegate>
 
@property (strong, nonatomic) UIWindow *window;
 
 
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//
//  ViewController.m
//  锁屏
//
//  Created by  on 2018/10/29.
//  Copyright © 2018 lzn. All rights reserved.
//
 
#import "ViewController.h"
#import "UnBlockView.h"
@interface ViewController ()
 
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UnBlockView *blockView = [[UnBlockView alloc]initWithFrame:self.view.frame];
    blockView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:blockView];
}
 
 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
 
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
//  UnBlockView.m
//  锁屏
//
//  Created by  on 2018/10/29.
//  Copyright © 2018 lzn. All rights reserved.
//
 
#import "UnBlockView.h"
 
@interface UnBlockView();
 
@property(nonatomic,assign) CGMutablePathRef path;
 
@end
 
@implementation UnBlockView
 
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = touches.anyObject;
    CGPoint pt = [touch locationInView:touch.view];
    //判断该点是不是落在了手势点上
    self.path = CGPathCreateMutable();
    CGPathMoveToPoint(self.path, NULL, pt.x, pt.y);
}
 
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = touches.anyObject;
    CGPoint pt = [touch locationInView:touch.view];
     
    CGPathAddLineToPoint(self.path, NULL, pt.x, pt.y);
    [self setNeedsDisplay];
}
 
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    if(self.path){
        CGPathRelease(self.path);
        self.path = nil;
    }
}
 
-(void)drawRect:(CGRect)rect{
    if(self.path){
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
        CGContextSetLineWidth(context, 4);
        CGContextDrawPath(context, kCGPathStroke);
    }
}
 
 
@end


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
Tender10 2018-10-29 13:55:29

1、touchesEnded:方法里,可以不用加if(self.path)判断

2、drawRect:方法里应该添加一个CGContextAddPath(context,self.path);


  • 提问者 SiuNam #1
    老师我已经按照你的修改完了,还是没有画出红线,能再帮我看看吗,谢谢!
    2018-10-29 13:58:26
  • 提问者 SiuNam #2
    老师我已经解决问题,是cgcontextaddpath的顺序出问题了,应该是提前!
    2018-10-29 14:12:58
  • Tender10 回复 提问者 SiuNam #3
    嗯嗯,这个位置是有关系的,解决了问题就好。
    2018-10-29 14:18:48
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码