UI 设计 绘图(涂鸦)

本文介绍了在Cocoa中如何处理触摸事件,主要关注UITouch类及其属性,如window、view、tapCount、timestamp和phase。通过示例展示了如何在自定义的RootView中实现画板功能,包括绘制线条、撤销操作,并监听触摸事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Cocoa中,代表触摸对象的类是UITouch。当用户触摸屏幕后,就会产生相应的事件,所有相关的UITouch对象都被包装在事件中,被程序交由特定的对象来处理。UITouch对象直接包括触摸的详细信息。

UITouch类中包含5个属性:

             window:触摸产生时所处的窗口。由于窗口可能发生变化,当前所在的窗口不一定是最开始的窗口。

             view:触摸产生时所处的视图。由于视图可能发生变化,当前视图也不一定时最初的视图。

             tapCount:轻击(Tap)操作和鼠标的单击操作类似,tapCount表示短时间内轻击屏幕的次数。因此可以根据tapCount判断单击、双击或更多的轻击。

             timestamp:时间戳记录了触摸事件产生或变化时的时间。单位是秒。

             phase:触摸事件在屏幕上有一个周期,即触摸开始、触摸点移动、触摸结束,还有中途取消。而通过phase可以查看当前触摸事件在一个周期中所处的状态。phase是UITouchPhase类型的,这是一个枚举配型,包含了

·      UITouchPhaseBegan(触摸开始)

·      UITouchPhaseMoved(接触点移动)

·      UITouchPhaseStationary(接触点无移动)

·      UITouchPhaseEnded(触摸结束)

·      UITouchPhaseCancelled(触摸取消)



#import "AppDelegate.h"

#import "RootViewController"

@interface AppDelegate ()

@end

@implementation AppDelegate

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

         self.window = [[UIWindow alloc] initWithFrame : [UIScreen mainScreen] .bounds];

         self.window.backgroundColor = [UIColor whiteColor];

         [self.window makeKeyAndVisible];


         self.window.rootViewController = [RootViewController new];

         return YES;

}


#import<UIKit / UIKit.h>

@interface RootViewController : UIViewController

@end


#import "RootViewController.h"

#import "RootView.h"

@interface RootViewController ()

@property (nonatomic,strong)RootView * rootView;

@end

@implementation RootViewController

-(void) loadView

{

          self.rootView = [RootView alloc] initWithFrame:[[UIScreen mainScreen].bounds];

          self.view = self.rootView;

}

-(void)viewDidLoad

{

         //创建一个继承于UIView的根视图,作为画板界面

         [super viewDidLoad];

         RootView *rootView =[ [RootView alloc] initWithFrame:self.view.bounds];

         rootView.backgroundColor = [UIColor lightGrayColor];

         [self.view addSubview:rootView];

}


#import <UIKit/UIKit.h>

@interface RootView : UIView


#pragram mark - 声明属性(声明一个可变数组,用来存储线上点)

@property(nonatomic,strong)NSMutableArray *LineArray;

@property(nonatomic,strong)UIButton *button;


@end


#import "RootView.h"

@implementation RootView

-(instancetype) initWithFrame:(CGRect)fame

{

         if(self = [super initWithFrame : frame])

         {

                // [self  addAllView];(此处做一个修改,在这之前涂鸦不上去)

                self.lineArray = [NSMutableArray arrayWithCapacity:1];

         }

         return self;

}

- (void)drawRect:(CGRect)rect //这个方法不能直接调用

{

         //得到上下文

         CGContextRef  context = UIGraphicsGetCurrentContext();

         //设置画笔的颜色

         CGContextSetStrokeColorWithColor(context , [UIColor redColor].CGColor);

         //设置画笔的粗细

         CGContextSetLineWidth(context , 2.0);

        //由于count是NSUInteger类型(无符号整形)所以转化成int型的,才不会出现数组越界

        for(int i = 0;i < [_lineArray count];i ++)

        {

                  NSMutableArray *pointArray = [_lineArray objectAtIndex:i];

                  for(int j = 0;j < (int)pointArray.count - 1;j ++)//当count - 1后,才能保证j + 1不会越界

                  {

                             NSValue * firstPointValue = [pointArray objectAtIndex:j];

                             NSValue * secondPointValue = [pointArray objectAtIndext:j + 1];


                             CGPoint firstPoint = [firstPointValue CGPointValue];

                             CGPoint secondPoint = [secondPointValue CGPointValue];

                             //把笔触移动到一个点

                             CGContextMoveToPoint(context,firstPoint.x,firstPoint.y);

                             //笔触和另一个点要连成一个路径

                             CGContextAddLineToPoint(context,secondPoint.x,secondPoint.y);

                  }

        }

        //绘制

        CGContextStrokPath(context);

}

//设置一个按钮,给按钮一个方法,当摁按钮的时候就会撤销最后画上去的那一笔

- (void)addAllView

{

         self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

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

         _button.backgroundColor = [UIColor purpleColor];

         _button.showsTouchWhenHighlighted = YES;

         [_button setTitle:@"撤销" forState:UIControlStateNormal];

         [_button addTarget:self action:@selector(undoAction:) forControlEvents:UIControlEventTouchUpInside];

         [self addSubview:_button];

}

-(void)undoAction:(UIButton *)sender

{

         [_lineArray  removeLastObject];

         [self setNeedsDisplay];//重新绘制

}

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

{

          NSMutableArray * pointArray = [NSMutableArray arrayWithCapacity:1];

          [_lineArray addObject:pointArray];

}

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

{

          UITouch *touch = [touches anyObject];

          CGPoint point = [touches anyObject];

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

          NSMutableArray *pointArray = [_lineArray lastObject];

          NSValue *pointValue = [NSValue valueWithCGPoint: point];

          [pointArray addObject:pointValue];

          //重绘界面

          [self setNeedsDisplay];

}

//如果用的是MRC,则要将数组release掉

-(void) dealloc

{

          [_lineArray release];

          [super dealloc];

}

@end



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值