zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    jQuery-1.样式篇---属性与样式
    jQuery-1.样式篇---选择器
    jQuery-1.样式篇
    随机数
    UIButton
    UILabel
    webView
    气泡聊天
    下拉和上拉刷新
    LimitDemo
  • 原文地址:https://www.cnblogs.com/rhcad/p/2221372.html
Copyright © 2011-2022 走看看