在iOS中使用手指简单画线

在iOS中使用手指简单画线,第1张

概述这个画线功能主要是为了辅助在iOS中支持手势锁屏的功能,哪位知道有现成的GestureLock项目的,求分享。 @interface ViewController ()@property (nonatomic, strong) UIImageView *imageView;@property (nonatomic, assign) CGPoint lineStartPoint;@prop

这个画线功能主要是为了辅助在iOS中支持手势锁屏的功能,哪位知道有现成的GestureLock项目的,求分享。

@interface VIEwController ()@property (nonatomic,strong) UIImageVIEw *imageVIEw;@property (nonatomic,assign) CGPoint linestartPoint;@property (nonatomic,assign) CGPoint lineEndPoint;@end

要画线,需要有画板imageVIEw,还有线的起始点、终点。

通过Cocoa touch支持的交互特性,我们可以跟踪用户手指点击和移动:

#pragma mark - Trace touch Point- (voID)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    CGPoint touchPoint;        UItouch *touch = [touches anyObject];    if (touch) {        touchPoint = [touch locationInVIEw:self.imageVIEw];        NSLog(@"touchesBegan : %f,%f\n",touchPoint.x,touchPoint.y);                self.linestartPoint = touchPoint;    }}- (voID)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{    CGPoint touchPoint;        UItouch *touch = [touches anyObject];    if (touch) {        touchPoint = [touch locationInVIEw:self.imageVIEw];        NSLog(@"touchesMoved : %f,touchPoint.y);                self.lineEndPoint = touchPoint;        self.imageVIEw.image = [self drawlineWithcolor:[UIcolor yellowcolor] wIDth:10.0f startPoint:self.linestartPoint endPoint:self.lineEndPoint];    }}- (voID)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{    ;}

最后就是基础的画线功能:

#pragma mark - Draw line- (UIImage *)drawlineWithcolor:(UIcolor *)color wIDth:(CGfloat)wIDth startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint{    UIImage *image = nil;        UIGraphicsBeginImageContext(self.imageVIEw.frame.size);        CGContextRef context = UIGraphicsGetCurrentContext();        CGContextSetlinewidth(context,wIDth);    CGContextSetstrokecolorWithcolor(context,[color CGcolor]);        CGContextMovetoPoint(context,startPoint.x,startPoint.y);    CGContextAddlinetoPoint(context,endPoint.x,endPoint.y);        CGContextstrokePath(context);        image = UIGraphicsGetimageFromCurrentimageContext();        UIGraphicsEndImageContext();        return image;}

这样,就可以根据手指移动来绘制线条了。

这个功能可以做一些趣味App,或者我的目的:手势锁屏和解锁。

如下是简单效果图:

总结

以上是内存溢出为你收集整理的在iOS中使用手指简单画线全部内容,希望文章能够帮你解决在iOS中使用手指简单画线所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1055063.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存