UI 设计 绘图(涂鸦)
2015-09-07 22:12
417 查看
在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
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
相关文章推荐
- 内核工作队列workqueue
- 搭建go开发环境时,出现GoSublime error: MarGo build failed的问题
- POJ3368--Frequent values
- ContentValues、HashTable、HashMap异同
- 黑马程序员-OC基础之NSValue的使用
- EasyUI -datagrid分页后台查询
- ByBueredStream
- [APUE] 再读之进程关系
- 在现实编程中使用fluent风格
- POJ 1947 Rebuilding Roads
- 《APUE》读书笔记—进程控制
- 禁止Ubuntu开机进入guest session
- unique函数
- LeetCode(60) Permutation Sequence
- LeetCode(60) Permutation Sequence
- Codeforces 156D Clues (图论, 并查集, 卡特兰数)
- UISearchBar的基本用法
- easyui 布局
- EasyUI中datagrid控件的Editor
- build.gradle 各种错误解决总结