小寒假第十四天总结 uitouch

来源:互联网 发布:c语言编写安卓 编辑:程序博客网 时间:2024/06/04 19:49

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

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    NSLog(@"touchesBegan");

    self.backgroundColor = [UIColorcolorWithRed:(arc4random() % (256))/255.0green:(arc4random() % (256))/255.0blue:(arc4random() % (256))/255.0alpha: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 = [superinitWithFrame:frame];

   if (self) {

        [selfsetLineArray:[NSMutableArrayarrayWithCapacity:1]];

        

        UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

        button.backgroundColor = [UIColorgreenColor];

        button.frame =CGRectMake((frame.size.width -100) / 2, frame.size.height -50, 100, 30);

        [button setTitle:@"undo"forState:UIControlStateNormal];

        [button addTarget:selfaction:@selector(undo:)forControlEvents:UIControlEventTouchUpInside];

        [selfaddSubview:button];

        

        

    }

    return self;

}


- (void)undo:(UIButton *)button

{

    [_lineArrayremoveLastObject];

    

    [selfsetNeedsDisplay];

}




// 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, [UIColorredColor].CGColor);

    //设置画笔的粗细

    CGContextSetLineWidth(context,4.0);

    

   for (int i =0; i < [_lineArraycount]; i++) {

       NSMutableArray *pointArray = [_lineArrayobjectAtIndex: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 = [NSMutableArrayarrayWithCapacity:3];

    [_lineArrayaddObject:pointArray];

    

}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

   UITouch *touch = [touches anyObject];

   CGPoint point = [touch locationInView:self];

    NSLog(@"point = %@",NSStringFromCGPoint(point));

    

   NSMutableArray *pointArray = [_lineArraylastObject];

   NSValue *pointValue = [NSValuevalueWithCGPoint:point];

    [pointArrayaddObject:pointValue];

    

   //重绘

    [selfsetNeedsDisplay];

    

}



- (void)dealloc

{

    [_lineArray release];

    

    [superdealloc];

}



0 0
原创粉丝点击