我开始使用XCode开发,我开始做一些练习和练习。我试图在iPhone中做一个简单的一个,一个绘图示例,一个触摸事件的板岩,但我做错了什么;我有点失落。
我创建了我的HDPizarra类,我正在努力保持这些动作。我有一个擦除按钮,一个IBAction必须清理平板并重新启动视图;我把线条用红色和宽度为2的线条,然后我把触摸的LocationInView的星形和结束。但它没有;它开始了视图但当我触摸屏幕时没有任何反应。这是代码:
-(IBAction)borrar: (id)sender
{
[lineas removeAllObjects];
}
- (id)initWithFrame: (CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
-(NSMutableArray *)lineas{
if (_lineas == nil) {
_lineas = [NSMutableArray new];
}
return _lineas;
}
- (void)drawRect: (CGRect)rect
{
if (!lineas) {
lineas = [[NSMutableArray alloc] initWithCapacity:0];
}
CGContextRef contexto = UIGraphicsGetCurrentContext();
UIColor * rojo = [UIColor redColor];
CGContextSetStrokeColorWithColor(contexto, rojo.CGColor);
CGContextSetLineWidth(contexto, 2);
CGContextMoveToPoint(contexto, inicio.x, inicio.y);
CGContextAddLineToPoint(contexto, fin.x, fin.y);
CGContextStrokePath(contexto);
}
-(void)touchesBegan: (NSSet *)touches withEvent: (UIEvent *)event{
UITouch * touch = [touches anyObject];
inicio = [touch locationInView:self];
fin = [touch locationInView:self];
[self setNeedsDisplay];
}
-(void)touchesMoved: (NSSet *)touches withEvent: (UIEvent *)event{
UITouch * touch = [touches anyObject];
fin = [touch locationInView:self];
HDLinea * linea = [[HDLinea alloc] initWithRect:CGRectMake(inicio.x, inicio.y, fin.x, fin.y)];
[self.lineas addObject: linea];
inicio.x = fin.x;
inicio.y = fin.y;
[self setNeedsDisplay];
}
移动来自HDLinea对象,在一个NSMutableArray中; .h的代码是:
#import <Foundation/Foundation.h>
@interface HDLinea : NSObject
@property (readwrite) CGRect puntos;
-(id) initWithRect: (CGRect) puntos;
@end
其中一个实现是:
#import "HDLinea.h"
@implementation HDLinea
@synthesize puntos = _puntos;
- (id)initWithRect: (CGRect)puntos
{
self = [super init];
if (self) {
self.puntos = puntos;
}
return self;
}
@end
有什么想法吗?非常感谢!!