小寒假第十四天总结 uitouch

实现 点击图片 变化颜色 与 拖动图片

- (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));

    

}




实现生成画板APP

- (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];

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值