【发布时间】:2013-05-01 00:03:38
【问题描述】:
我找到了我想在我的视图控制器中实现的教程,但代码是用UIView 编写的,所以如何将UIView 代码转换为UIViewController 它可以在这里工作是我的代码
#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
}
【问题讨论】:
-
最简单的方法是将视图添加为视图控制器的子视图...... drawRect 方法也适用于视图......
-
@MountainLion 你能告诉我不制作 subView 如果我想制作的话,我该如何在 ViewController 中编写这段代码
标签: iphone xcode ipad uiview uiviewcontroller