我开始使用 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];
}
这些移动来自一个 NSMutableArray 中的 HDLinea 对象;.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
任何想法?非常感谢!!