实现 点击图片 变化颜色 与 拖动图片
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touchesBegan");
self.backgroundColor = [UIColor colorWithRed:(arc4random() % (256))/255.0 green:(arc4random() % (256))/255.0 blue:(arc4random() % (256))/255.0 alpha:0.6];
UITouch *touch = [touches anyObject];
_starPoint = [touch locationInView:self];
NSLog(@"point = %@", NSStringFromCGPoint(_starPoint));
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touchesCancelled");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touchesEnded");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touchesMoved");
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self];
CGFloat offSetX = currentPoint.x - _starPoint.x;
CGFloat offSetY = currentPoint.y - _starPoint.y;
NSLog(@"offSetX = %f, offSetY = %f", offSetX, offSetY);
CGPoint newCenter = self.center;
newCenter.x += offSetX;
newCenter.y += offSetY;
self.center = newCenter;
NSLog(@"currentPoint = %@", NSStringFromCGPoint(currentPoint));
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setLineArray:[NSMutableArray arrayWithCapacity:1]];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.backgroundColor = [UIColor greenColor];
button.frame = CGRectMake((frame.size.width - 100) / 2, frame.size.height - 50, 100, 30);
[button setTitle:@"undo" forState:UIControlStateNormal];
[button addTarget:self action:@selector(undo:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
}
return self;
}
- (void)undo:(UIButton *)button
{
[_lineArray removeLastObject];
[self setNeedsDisplay];
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
//得到上下文 绘图中需要的设置
CGContextRef context = UIGraphicsGetCurrentContext();
//设置画笔的颜色
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
//设置画笔的粗细
CGContextSetLineWidth(context, 4.0);
for (int i = 0; i < [_lineArray count]; i++) {
NSMutableArray *pointArray = [_lineArray objectAtIndex:i];
for (int j = 0; j < (int)pointArray.count - 1; j++) {
NSValue *firstPointValue = [pointArray objectAtIndex:j];
NSValue *secondPointValu = [pointArray objectAtIndex:j + 1];
CGPoint firstPoint = [firstPointValue CGPointValue];
CGPoint secondPoint = [secondPointValu CGPointValue];
//把笔触移动一个点
CGContextMoveToPoint(context, firstPoint.x, firstPoint.y);
//笔触和另一个点形成一个路径
CGContextAddLineToPoint(context, secondPoint.x, secondPoint.y);
}
}
//绘制
CGContextStrokePath(context);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSMutableArray *pointArray = [NSMutableArray arrayWithCapacity:3];
[_lineArray addObject:pointArray];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
NSLog(@"point = %@", NSStringFromCGPoint(point));
NSMutableArray *pointArray = [_lineArray lastObject];
NSValue *pointValue = [NSValue valueWithCGPoint:point];
[pointArray addObject:pointValue];
//重绘
[self setNeedsDisplay];
}
- (void)dealloc
{
[_lineArray release];
[super dealloc];
}