- 浏览: 763357 次
- 性别:
- 来自: 天堂
文章分类
最新评论
-
xiaozhao-521:
呀呀呀呀呀呀呀
RequestTest222 -
Andy_hyh:
打扰了,问下openmeeting源码可以运行起来吗?
Openmeetings安装 详细步骤 -
qindongliang1922:
擦,现在还行么,厉害
北京免费吃饭的地方 -
minixx77:
...
Openmeetings安装 详细步骤 -
wwwqqqiang:
喜欢楼主分享问题的方式,有思想
UIView 和 CALayer的那点事
tap是指轻触手势。类似鼠标操作的点击。从iOS 3.2版本开始支持完善的手势api:
- tap:轻触
- long press:在一点上长按
- pinch:两个指头捏或者放的操作
- pan:手指的拖动
- swipe:手指在屏幕上很快的滑动
- rotation:手指反向操作
这为开发者编写手势识别操作,提供了很大的方便,想想之前用android写手势滑动的代码(编写android简单的手势切换视图示例),尤其感到幸福。
这里写一个简单的tap操作。在下面视图的蓝色视图内增加对tap的识别:
当用手指tap蓝色视图的时候,打印日志输出:
代码很简单,首先要声明tap的recognizer:
UITapGestureRecognizer *recognizer=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
[infoView addGestureRecognizer:recognizer];
[recognizer release];
在这里:
- initWithTarget:self,要引用到Controller,因为一般这部分代码写在controller中,用self;
- action:@selector(handleTapFrom:),赋值一个方法名,用于当手势事件发生后的回调;
- [infoView addGestureRecognizer:recognizer],为view注册这个手势识别对象,这样当手指在该视图区域内,可引发手势,之外则不会引发
对应的回调方法:
-(void)handleTapFrom:(UITapGestureRecognizer *)recognizer{
NSLog(@">>>tap it");
}
controller相关方法完整的代码(包含了一些与本文无关的视图构建代码):
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
//去掉最顶端的状态拦
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation: UIStatusBarAnimationSlide];
UIImage *image=[UIImage imageNamed:@"3.jpg"];
//创建背景视图
self.view=[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
UIImageView *backgroudView=[[UIImageView alloc] initWithImage:image];
[self.view addSubview:backgroudView];
/*
UIToolbar *toolBar=[[UIToolbar alloc] initWithFrame:CGRectMake(0, 1024-70, 768, 70)];
toolBar.alpha=0.8;
toolBar.tintColor = [UIColor colorWithRed:.3 green:.5 blue:.6 alpha:.1];
NSArray *items=[NSArray arrayWithObjects:[[UIBarButtonItem alloc] initWithTitle:@"test" style:UIBarButtonItemStyleDone target:self action:nil],nil];
toolBar.items=items;
[self.view addSubview:toolBar];
*/
UIView *bottomView=[[UIView alloc] initWithFrame:CGRectMake(0, 1024-70, 768, 70)];
bottomView.backgroundColor=[UIColor grayColor];
bottomView.alpha=0.8;
//UIButton *backButton=[[UIButton alloc] initWithFrame:CGRectMake(10, 10, 100, 40)];
UIButton *backButton=[UIButton buttonWithType: UIButtonTypeRoundedRect];
[backButton setTitle:@"ok" forState:UIControlStateNormal];
backButton.frame=CGRectMake(10, 15, 100, 40);
[bottomView addSubview:backButton];
[self.view addSubview:bottomView];
UIView *infoView=[[UIView alloc] initWithFrame:CGRectMake(200, 700, 768-400, 70)];
infoView.backgroundColor=[UIColor blueColor];
infoView.alpha=0.6;
infoView.layer.cornerRadius=6;
infoView.layer.masksToBounds=YES;
[self.view addSubview:infoView];
UITapGestureRecognizer *recognizer=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
[infoView addGestureRecognizer:recognizer];
[recognizer release];
}-(void)handleTapFrom:(UITapGestureRecognizer *)recognizer{
NSLog(@">>>tap it");
}
翻页效果,类似下面的样子:
在电子书应用中会很常见。这里需要两个要点:
- 翻页动画
- 手势上下轻扫(swipe)的处理
先说一下轻扫(swipe)的实现,可以参考编写简单的手势示例:Tap了解手势种类。
在viewDidLoad方法中注册了对上、下、左、右四个方向轻松的处理方法:
- (void)viewDidLoad {
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
[super viewDidLoad];
可以看到,都是同一个方法,handleSwipeFrom。
在该方法中,再识别具体是哪个方向的轻扫手势,比如判断是向下的轻扫:
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
NSLog(@"Swipe received.");
if (recognizer.direction==UISwipeGestureRecognizerDirectionDown) {
NSLog(@"swipe down");判断是向上的轻扫:
if (recognizer.direction==UISwipeGestureRecognizerDirectionUp) {
NSLog(@"swipe up");有关动画的处理,比如向下(往回)翻页,类似这样:
[UIView beginAnimations:@"animationID" context:nil];
[UIView setAnimationDuration:0.7f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationRepeatAutoreverses:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];[currentView removeFromSuperview];
[self.view addSubview:contentView];[UIView commitAnimations];
向上(向前)翻页,只需改为:
[UIView beginAnimations:@"animationID" context:nil];
[UIView setAnimationDuration:0.7f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationRepeatAutoreverses:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];[currentView removeFromSuperview];
[self.view addSubview:contentView];[UIView commitAnimations];
如果是电子书,还需要考虑一个问题,就是有多个页面(图形),比如50页。那么需要有一个数据结构来保存这些页面的图片路径:
- objc数据结构,比如数组
- sqlite数据库表
这样,写一套翻页代码和加载什么图形之间就可以解耦。
本文示例使用的是数组,类似这样:
pages=[[NSArray alloc] initWithObjects:@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",
nil];图片保存在resources下。
为了能让上页下页翻页的时候找到关联的页面,采用了如下机制:
- 将图片封装为UIImageView显示
- 可以为UIImageView设置一个tag值,值为数组下标+1
- 这样,上级view有方法能根据tag查询到UIImageView,比如:UIView *currentView=[self.view viewWithTag:currentTag];
- 设置一个成员变量currentTag保存当前的tag值
比如这样,当应用加载的时候显示第一页:
currentTag=1;
NSString *path = [[NSBundle mainBundle] pathForResource:@"pageflip1" ofType:@"mp3"];
player=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
//[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation: UIStatusBarAnimationSlide];
UIImageView *contentView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[contentView setImage:[UIImage imageNamed:[pages objectAtIndex:(currentTag-1)]]];
[contentView setUserInteractionEnabled:YES];
contentView.tag=currentTag;在翻页时的处理:
if (currentTag<[pages count]) {
UIView *currentView=[self.view viewWithTag:currentTag];
currentTag++;
UIImageView *contentView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[contentView setImage:[UIImage imageNamed:[pages objectAtIndex:(currentTag-1)]]];
[contentView setUserInteractionEnabled:YES];
contentView.tag=currentTag;
[UIView beginAnimations:@"animationID" context:nil];
[UIView setAnimationDuration:0.7f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationRepeatAutoreverses:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
[currentView removeFromSuperview];
[self.view addSubview:contentView];
[UIView commitAnimations];
发表评论
-
iOS 自定义UIActionSheet
2012-12-18 16:07 16375一:模态视图 UIActi ... -
UIView 和 CALayer的那点事
2012-11-17 23:51 30627UIView 和 CALayer的那点事 (1 ... -
iOS Open Source : Popover API for iPhone
2012-01-20 15:02 1903http://iphonedevelopertips.com/ ... -
ios 任务、线程、定时器
2011-12-26 18:09 7977一:operations(任务) cocoa提供了三种 ... -
ios url缓存策略——NSURLCache、 NSURLRequest
2011-12-26 17:09 24310一:url 缓存策略 NSURLRequest ... -
ios NSInvocation简单使用
2011-12-22 16:39 6320在ios直接调用某个对象的消息是方法有两种: 一:perfo ... -
iphone 对Web Services的三种请求方式soap get post
2011-11-09 10:57 6373一:Using SO AP 1.1 POST / ... -
sdk3.2手势实例
2011-11-09 10:11 1704#import <UIKit/UIKit.h>@i ... -
关于iphone 利用hpple解析html的问题
2011-08-04 18:28 2171最近在用happe解析html中的图片。有个翻页操作,如果请 ... -
iphone hpple 解析html,xml
2011-07-19 16:21 2708使用Objective-C解析HTML或者XML,系统自带有两 ... -
激活 iPhone通过 GPRS 连接服务器功能的代码
2011-05-13 15:14 1611如果您的 iPhone 应用里含有连接服务器的功能,也许会遇到 ... -
address book api 图型
2011-04-28 15:51 1106最近要搞地址簿了,整理一下 -
[OmniGraffle]iPhone app原型制作工具
2011-04-06 17:35 3914在写程序之前,我们通常需要做一些mockup出来(不知道款爷有 ... -
自定义uislider 样式
2011-04-04 21:28 3795UIImage *stetchLeftTrack= [[UII ... -
iphone 下AsyncSocket网络库编程
2011-04-02 21:04 7589iphone的标准推荐CFNetwork ... -
进阶AlertView运用 - 登入设计
2011-04-01 17:52 2994说明:示范如何利用AlertView来制作系统登入的介面程式碼 ... -
iPad UIPopoverController弹出窗口的位置和坐标
2011-04-01 17:42 1959优化规则: TodoViewControlle ... -
iPhone系统自动化测试
2011-04-01 17:39 2575首先mac系统是必备的2 安装iPhone SD ... -
iphone上面编写具有root权限的程序
2011-04-01 17:31 6244正常途径下, 我们编写的程序发布在App store上, 使用 ... -
聊天。。。。。
2011-04-01 17:13 1054是得分手段
相关推荐
手势 滑动 Demo IOS Gesture 代码实例
2. UISwipeGestureRecognizer 3. UIPanGestureRecognizer 4. UIPinchGestureRecognizer 5. UIRotationGestureRecognizer 6. UILongPressGestureRecognizer 7. UIScreenEdgePanGestureRecognizer 详细介绍查看这篇...
UIKit中包含了UIGestureRecognizer类,用于检测发生在设备中的... 4、擦碰UISwipeGestureRecognizer (以任意方向) 5、旋转UIRotationGestureRecognizer (手指朝相反方向移动) 6、长按UILongPressGestureRecognizer
1、使用自定义UITableViewCell + UISwipeGestureRecognizer + 代理 实现; 2、使用自定义UITableViewCell + UIPanGestureRecognizer + 代理 实现; 3、使用自定义UITableViewCell + UISwipeGestureRecognizer + ...
###? 这个宝石可让您在任何严肃的应用程序中隐藏一点乐趣。... add_egg ( number_touches : 2 , image_file : "taco.png" ) 可自定义的选项和默认值为: secret_code: UISwipeGestureRecognizer常数数
一.前言IOS监听手势使用的方法为UISwipeGestureRecognizer。二. 添加手势监听三. 添加响应事件四. 模板把上面的整合起来,基本可以按照
本文实例为大家分享了ios实现左右滑动操作代码,供大家参考,具体内容如下 一、效果图 二、代码 RootViewController.m - (void)viewDidLoad ... recognizerLeft = [[UISwipeGestureRecognizer a
IOS 开发之swift中手势的实例详解 ...滑动 UISwipeGestureRecognizer direction:滑动方向 direction 滑动方向分为上Up、下Down、左Left、右Right 拖动 UIPanGestureRecognizer 在拖动过程中,通过方
先看下效果 ...5、轻扫 UISwipeGestureRecognizer 我们上面这个实例中就用到了上面这5种手势,不过其中 点击与轻扫没有体现出来,只是输出了下日志而已,一会看代码 下面我们来分别介绍下这几种手势
作者xiaotanit,源码Tan_UITableViewCellLeftSwipe,Tan_UITableViewCellLeftSwipe: 自定义UITableViewCell, 实现左滑动多菜单功能,先上图:...3、自定义UITableViewCell + UISwipeGestureRecognizer + block 来实现
来源: github/Tan_UITableViewC Licence: MIT 作者: ECF_铭_ ... 使用了三种方式来实现该功能: 1、自定义UITableViewCell + UISwipeGestureRecognizer + 代理 来实现; 2、自定义UITableViewCell + UIPanGestureRec