我找到了我想在我的视图控制器中实现的教程,但是代码是用编写的,UIView
所以如何将UIView
代码转换为UIViewControlle
r 它可以在这里工作是我的代码
#import <UIKit/UIKit.h>
@interface MyLineDrawingView : UIView {
UIBezierPath *myPath;
UIColor *brushPattern;
}
@end
#import "MyLineDrawingView.h"
@implementation MyLineDrawingView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor=[UIColor whiteColor];
myPath=[[UIBezierPath alloc]init];
myPath.lineCapStyle=kCGLineCapRound;
myPath.miterLimit=0;
myPath.lineWidth=10;
brushPattern=[UIColor redColor];
}
return self;
}
drawRect:
仅当您执行自定义绘图时才覆盖。空实现会对动画期间的性能产生不利影响。
- (void)drawRect:(CGRect)rect
{
[brushPattern setStroke];
[myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
// Drawing code
//[myPath stroke];
}
#pragma mark - Touch Methods
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath moveToPoint:[mytouch locationInView:self]];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath addLineToPoint:[mytouch locationInView:self]];
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//handle touch event
}