Quartz 2D 练习2--多点触摸画圈
总结:
1、在iTouch4上运行检测到的触摸点数最大是5,即只允许5个手指在屏幕上滑动
2、视图支持多点触摸:isMultipleTouchEnabled返回YES,或者设置 multipleTouchEnabled属性为YES
3、要将多个CGPoint点添加到NSMutableArray,使用 NSStringFromCGPoint 函数,使用 CGPointFromString 从数组中取出坐标
TouchesView.h
#import <UIKit/UIKit.h>
@interface TouchesView : UIView {
    NSMutableArray* _points;
}
- (void)showPoints:(NSSet *)touches;
@end
TouchesView.m
#import "TouchesView.h"
@implementation TouchesView
- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    _points = [[NSMutableArray alloc] init];
    return self;
}
- (void)dealloc
{
    [_points release];
    [super dealloc];
}
- (BOOL)isMultipleTouchEnabled
{
    return YES;
}
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    for (NSString* ptstr in _points) {
        CGPoint pt = CGPointFromString(ptstr);
        CGContextStrokeEllipseInRect(context, CGRectMake(pt.x - 40, pt.y - 40, 80, 80));
    }
}
- (void)showPoints:(NSSet *)touches
{
    [_points removeAllObjects];
    
    for (UITouch* touch in touches) {
        CGPoint pt = [touch locationInView:self];
        [_points addObject: NSStringFromCGPoint(pt)];
    }
    NSLog(@"Touches count: %d", touches.count);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    assert(_points != Nil);
    [self showPoints: touches];
    [self setNeedsDisplay];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self showPoints: touches];
    [self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    //[_points removeAllObjects];
    [self setNeedsDisplay];
}
@end
 
                    
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号