iphone 开发周记2 触摸事件处理与简单绘图。
2010-09-26 21:31
344 查看
iphone中由touchesBegan,touchesMoved,touchesEnded三个函数处理触摸事件。
分别代表开始触摸,移动,离开屏幕三个事件。
iphone中一般绘图在drawrect中进行。
下面是一段实现移动手指,当向不同方向移动时显示不同方向的箭头的代码。
#import "draw.h"
@implementation draw
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect {
// Drawing code
UIImage *image =[UIImage imageNamed:@"aa.gif"];
[image drawAtPoint:CGPointMake(p.x-30, p.y-30)];
if (move==LEFT) {
UIImage *imagemove =[UIImage imageNamed:@"left.png"];
[imagemove drawAtPoint:CGPointMake(50, 120) blendMode:kCGBlendModeNormal alpha:0.5f];
}
if (move==RIGHT) {
UIImage *imagemove =[UIImage imageNamed:@"right.png"];
[imagemove drawAtPoint:CGPointMake(50, 120) blendMode:kCGBlendModeNormal alpha:0.5f];
}
if (move==UP) {
UIImage *imagemove =[UIImage imageNamed:@"upp.png"];
[imagemove drawAtPoint:CGPointMake(50, 120) blendMode:kCGBlendModeNormal alpha:0.5f];
}
if (move==DOWN) {
UIImage *imagemove =[UIImage imageNamed:@"down.png"];
[imagemove drawAtPoint:CGPointMake(50, 120) blendMode:kCGBlendModeNormal alpha:0.5f];
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
p.x=location.x;
p.y=location.y;
touchstart.x=p.x;
touchstart.y=p.y;
[self setNeedsDisplay];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
p.x=location.x;
p.y=location.y;
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
CGPoint end;
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
end.x=location.x;
end.y=location.y;
if (end.x-touchstart.x>10.0f&&end.y-touchstart.y<50.0f&&end.y-touchstart.y>-50.0f) {
move=RIGHT;
}
if (end.x-touchstart.x<-10.0f&&end.y-touchstart.y<50.0f&&end.y-touchstart.y>-50.0f) {
move=LEFT;
}
if (end.y-touchstart.y>10.0f&&end.x-touchstart.x<50.0f&&end.x-touchstart.x>-50.0f) {
move=DOWN;
}
if (end.y-touchstart.y<-10.0f&&end.x-touchstart.x<50.0f&&end.x-touchstart.x>-50.0f) {
move=UP;
}
[self setNeedsDisplay];
}
- (void)dealloc {
[super dealloc];
}
@end
首先要建立一个基于uiview的名叫draw的类。
其中draw.h
#import <UIKit/UIKit.h>
#define LEFT 1
#define RIGHT 2
#define UP 3
#define DOWN 4
@interface draw : UIView {
CGPoint p;
CGPoint touchstart;
int move;
}
@end
然后打开IB,将viewcontoller中的主视图命名为draw。
再加载自定义的图片(箭头)。
点击运行:
当我拖动小狗向右时出现了向右的箭头。
分别代表开始触摸,移动,离开屏幕三个事件。
iphone中一般绘图在drawrect中进行。
下面是一段实现移动手指,当向不同方向移动时显示不同方向的箭头的代码。
#import "draw.h"
@implementation draw
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect {
// Drawing code
UIImage *image =[UIImage imageNamed:@"aa.gif"];
[image drawAtPoint:CGPointMake(p.x-30, p.y-30)];
if (move==LEFT) {
UIImage *imagemove =[UIImage imageNamed:@"left.png"];
[imagemove drawAtPoint:CGPointMake(50, 120) blendMode:kCGBlendModeNormal alpha:0.5f];
}
if (move==RIGHT) {
UIImage *imagemove =[UIImage imageNamed:@"right.png"];
[imagemove drawAtPoint:CGPointMake(50, 120) blendMode:kCGBlendModeNormal alpha:0.5f];
}
if (move==UP) {
UIImage *imagemove =[UIImage imageNamed:@"upp.png"];
[imagemove drawAtPoint:CGPointMake(50, 120) blendMode:kCGBlendModeNormal alpha:0.5f];
}
if (move==DOWN) {
UIImage *imagemove =[UIImage imageNamed:@"down.png"];
[imagemove drawAtPoint:CGPointMake(50, 120) blendMode:kCGBlendModeNormal alpha:0.5f];
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
p.x=location.x;
p.y=location.y;
touchstart.x=p.x;
touchstart.y=p.y;
[self setNeedsDisplay];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
p.x=location.x;
p.y=location.y;
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
CGPoint end;
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
end.x=location.x;
end.y=location.y;
if (end.x-touchstart.x>10.0f&&end.y-touchstart.y<50.0f&&end.y-touchstart.y>-50.0f) {
move=RIGHT;
}
if (end.x-touchstart.x<-10.0f&&end.y-touchstart.y<50.0f&&end.y-touchstart.y>-50.0f) {
move=LEFT;
}
if (end.y-touchstart.y>10.0f&&end.x-touchstart.x<50.0f&&end.x-touchstart.x>-50.0f) {
move=DOWN;
}
if (end.y-touchstart.y<-10.0f&&end.x-touchstart.x<50.0f&&end.x-touchstart.x>-50.0f) {
move=UP;
}
[self setNeedsDisplay];
}
- (void)dealloc {
[super dealloc];
}
@end
首先要建立一个基于uiview的名叫draw的类。
其中draw.h
#import <UIKit/UIKit.h>
#define LEFT 1
#define RIGHT 2
#define UP 3
#define DOWN 4
@interface draw : UIView {
CGPoint p;
CGPoint touchstart;
int move;
}
@end
然后打开IB,将viewcontoller中的主视图命名为draw。
再加载自定义的图片(箭头)。
点击运行:
当我拖动小狗向右时出现了向右的箭头。
相关文章推荐
- iphone应用程序开发指南--触摸事件
- iPhone开发--检查UIWebView上Touch/UITapGestureRecognizer事件的最简单的方法(转载)
- iPhone开发进阶(8)— 检测屏幕触摸事件
- iphone应用程序开发指南--触摸事件
- 移动开发:iphone开发之触摸事件详解
- Iphone开发(十)简单的列表tableView与行的响应事件
- 简单讲解Android开发中触摸和点击事件的相关编程方法
- iphone开发之触摸事件详解
- 移动开发:iphone开发之触摸事件详解
- iPhone开发 当输入的键盘会挡住UITextField的处理事件
- iphone开发之事件处理详解
- iPhone开发指南-事件处理
- Android官方开发文档Training系列课程中文版:OpenGL绘图之响应触摸事件
- iOS开发——UI进阶篇(十二)事件处理,触摸事件,UITouch,UIEvent,响应者链条,手势识别
- <WEB>平板_手机开发_13 个处理触摸事件和多点触摸的JS 库
- 【React Native开发】- 触摸事件处理
- Iphone开发(十)简单的列表tableView与行的响应事件
- iOS开发笔记--iOS中的触摸事件和手势处理
- Android开发--事件的处理/按键按下,弹起,触摸事件等
- Iphone开发(十)简单的列表tableView与行的响应事件