我找到了我想在我的视图控制器中实现的教程,但代码是用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 *)触及withEvent:(UIEvent *)事件 {
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath addLineToPoint:[mytouch locationInView:self]];
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//handle touch event
}
答案 0 :(得分:3)
创建UIViewController
对象并将view属性设置为此视图。这样的事情应该有效。
UIViewController *vc = [[UIViewController alloc] init];
MyLineDrawingView *myView = [[MyLineDrawingView alloc] init];
vc.view = myView;