自定义的ios tableviewcelll里面的imageView怎么接受touch事件

14652人阅读
IOS(137)
在Iphone开发中,像UIimageView是不支持点击的,但往往我们却有很多能在Image上点击的需求,比如一个自定义的TableViewCell中放入三个UIimageView,在这里命名为imageleft,imagemiddle,imggeright,当tableView加载后,单击tableView中某一行中的image,我便进入该图片的详细页面。
当然,现在的最新版支持手势控件,只要拖一个这样的控件到UIImageView上,实现它的委托就可以了。若版本太低不支持这样的控件,你便只好老老实实的亲手写代码了。
#import &UIKit/UIKit.h&
@protocol TableGridViewCellD
@interface TableGridViewCell : UITableViewCell {
@property (nonatomic,retain) IBOutlet UIImageView *
@property (nonatomic,retain) IBOutlet UIImageView *
@property (nonatomic,retain) IBOutlet UIImageView *
@property (nonatomic,assign) id&TableGridViewCellDelegate&
- (void) configG
- (void) handTap:(UITapGestureRecognizer*)...
@protocol TableGridViewCellDelegate &NSObject&
- (void) tapedImageViewInCell:(UITableViewCell*)cell withIndex:(int)
#import &TableGridViewCell.h&
@implementation TableGridViewCell
@synthesize imageleft,imagemiddle,imageright,
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
[super setSelected:selected animated:animated];
// Configure the view for the selected state
-(void) dealloc{
SAFE_RELEASE(imageleft);
SAFE_RELEASE(imagemiddle);
SAFE_RELEASE(imageright);
[super dealloc];
- (void) configGesture
UITapGestureRecognizer *_left = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handTap:)];
[imageleft addGestureRecognizer:_left];
SAFE_RELEASE(_left);
UITapGestureRecognizer *_mid = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handTap:)];
[imagemiddle addGestureRecognizer:_mid];
SAFE_RELEASE(_mid);
UITapGestureRecognizer *_right = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handTap:)];
[imageright addGestureRecognizer:_right];
SAFE_RELEASE(_right);
- (void) handTap:(UITapGestureRecognizer*) gesture
if ([delegate respondsToSelector:@selector(tapedImageViewInCell:withIndex:)]) {
UIImageView *v = (UIImageView*)gesture.
[delegate tapedImageViewInCell:self withIndex:v.tag];
#pragma mark -#pragma mark TableGridViewCellDelegate- (void) tapedImageViewInCell:(UITableViewCell*)cell withIndex:(int)index{&&& int row = [myTable indexPathForCell:cell].&&& VODItem *it = (VODItem*)[ipListarry objectAtIndex:row*3+index];&&& NSString *preview_Url = [[NSString alloc] initWithFormat:@&%@&,it.Preview_Url];&&& IPCellDetailInfo *IPCellDetailInfoController = [[IPCellDetailInfo alloc]init];&&& IPCellDetailInfoController.ClipDetialsInterfaceUrl=it.ClipDetialsInterfaceU&&& IPCellDetailInfoController.ClipDetialsTitle = it.Primary_N&&& IPCellDetailInfoController.btnPlayOrViewTitle = it.Sndlvl_D  &&& [self.navigationController pushViewController:IPCellDetailInfoController animated:YES]; //新视图压入到栈中&&& [IPCellDetailInfoController release];&& &&&& NSLog(@&row = [%d], col = [%d], Preview_Url = [%@]&, row, index,preview_Url);&&& [preview_Url release];}
好了& 其实主要就是要会使用UITapGestureRecognizer,当然这只是手势的其中一个。下面还有几个如:
UITapGestureRecognizer
UIPinchGestureRecognizer
UIRotationGestureRecognizer
UISwipeGestureRecognizer
UIPanGestureRecognizer
UILongPressGestureRecognizer
从命名上不难了解這些类別所对应代表的手势,分別是 Tap(点一下)、Pinch(二指往內或往外拨动)、Rotation(旋转)、Swipe(滑动,快速移动)、Pan (拖移,慢速移动)以及 LongPress(长按)。
比如一个很简单的开关实现,使用UIImageView的手势来实现,这种情况一般加在TableViewCell里面很好用
-(void)handleTap:(id)sender{
& &&UITapGestureRecognizer&*tap =
& &&UIImageView&*imgView = (UIImageView*)tap.view;
& & [imgView&setHighlighted:!imgView.highlighted];
-(UITableViewCell&*)tableView:(UITableView&*)tableView cellForRowAtIndexPath:(NSIndexPath&*)indexPath{
& &&[imgView&setImage:[UIImage&imageNamed:@&on.png&]];
& & [imgView&setHighlightedImage:[UIImage&imageNamed:@&off.png&]];
& &&UITapGestureRecognizer&*tap = [[UITapGestureRecognizer&alloc]initWithTarget:self&action:@selector(handleTap:)];
& & [imgView&addGestureRecognizer:tap];
& & [imgView&setUserInteractionEnabled:YES];
& & [self.view&addSubview:imgView];
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:842149次
积分:7763
积分:7763
排名:第1975名
原创:115篇
转载:135篇
评论:60条
(1)(1)(1)(15)(2)(2)(5)(1)(3)(1)(6)(2)(1)(1)(5)(3)(3)(1)(1)(1)(2)(1)(3)(6)(3)(11)(1)(19)(28)(22)(29)(37)(35)在此之前,我们已经创建了一个通过简单的表视图应用程序并显示预定义的图像。在本教程中,我们将继续努力,使应用程序变得更好,:
&不同的行显示不同的图像 - 上个教程,我们的所有行显示相同的缩略图。那么不同的食物显示不同的图片不是更好么?
&自定义视图单元-我们将展示我们自己的视图来替代默认表单元格样式
显示不同缩略图
在我们更改代码之前,让我们回顾显示缩略图的代码。
最后,我们增加了一个行代码指示UITableView每一行显示“creme_brelee.jpg”这张图片。显然,为了显示不同的图像,我们需要改变这行代码。正如之前解释的那样,IOS在显示一条表单元格时自动调用“cellForRowAtIndexPath”方法
-&(UITableViewCell&*)tableView:(UITableView&*)tableView
cellForRowAtIndexPath:(&*)indexPath
看看该方法的参数,每次调用时通过“indexPath”来传递表单元格信息。indexPath参数包含的表单元格的行数(以及节号)。您可以简单地使用“indexPath.row”属性,找出当前指向哪一行。和数组一样,表行的计数从零开始。换言之,对于第一行“indexPath.row”返回0。
因此,为了显示不同的缩略图,我们将添加一个新的数组(即缩略图),存储文件名的缩略图:
@implementation&SimpleTableViewController&&
&&&&&*tableD&&
-&(void)viewDidLoad&&
&&&&[super viewDidLoad];&&
&&&&// Initialize table data&
&&& tableData&=&[&arrayWithObjects:@&Egg
Benedict&,&@&Mushroom Risotto&,&@&Full
Breakfast&,&@&Hamburger&,&@&Ham
and Egg Sandwich&,&@&Creme Brelee&,&@&White
Chocolate Donut&,&@&Starbucks Coffee&,&@&Vegetable
Curry&,&@&Instant Noodle with Egg&,&@&Noodle
with BBQ Pork&,&@&Japanese Noodle with Pork&,&@&Green
Tea&,&@&Thai Shrimp Cake&,&@&Angry
Birds Cake&,&@&Ham and Cheese Panini&,&nil];&&
&&&&// Initialize thumbnails&
&&& thumbnails&=&[&arrayWithObjects:@&egg_benedict.jpg&,&@&mushroom_risotto.jpg&,&@&full_breakfast.jpg&,&@&hamburger.jpg&,&@&ham_and_egg_sandwich.jpg&,&@&creme_brelee.jpg&,&@&white_chocolate_donut.jpg&,&@&starbucks_coffee.jpg&,&@&vegetable_curry.jpg&,&@&instant_noodle_with_egg.jpg&,&@&noodle_with_bbq_pork.jpg&,&@&japanese_noodle_with_pork.jpg&,&@&green_tea.jpg&,&@&thai_shrimp_cake.jpg&,&@&angry_birds_cake.jpg&,&@&ham_and_cheese_panini.jpg&,&nil];&&
正如你可以从上面的代码中看到的,我们用图像文件名列表初始化了的缩略图数组。图像的顺序排列与“tableData”对齐。
为了您的方便,您可以下载并把它们添加到您的项目,确保“Copy
items into destination group’s folder”已勾选当然你也可以用自己的图片。
添加的图像文件后,你应该会在Project Navigator中发现他们,如下面的屏幕:
最后,更改在cellForRowAtIndexPath“方法的代码:
cell.imageView.image&=&[&UIImage的imageNamed&:[缩略图objectAtIndex&:&indexPath.row&]&]&;
什么是&[thumbnails objectAtIndex:indexPath.row]?
这行代码获取特定行的图像名字。就是说,indexPath.row属性对于第一行返回0,我们使用“objectAtIndex”的方法选取图像数组的第一个(即egg_benedict.jpg)。
保存所有的更改后,再次尝试运行您的应用程序。现在应该不同的表中单元格显示缩略图:
自定义表格视图单元格
应用程序是否可以更好看?我们将通过自定义表格单元格使其更好看。到目前为止,我们使用的默认样式显示表单元格,而且缩略图的位置和大小是固定的。如果你想就像下面的屏幕显示的那样使缩略图更大并附上每个菜的准备时间,怎么办?
不同风格的自定义表格视图单元格
设计单元格
在这种情况下,你必须创建和设计自己的表格单元格。返回到Xcode。在项目浏览器中,右键单击“SimpleTable”文件夹并选择“New
File ...”。
为了设计我们自己的表格单元格,我们要创建一个新的表格单元格界面生成器。在这种情况下,我们只需要启动一个“空”的用户界面。点击“下一步”继续。
选择一个空的Interface Builder文档
当提示选择设备系列时,选择“iPhone”,然后单击“下一步”继续。保存的文件为“SimpleTableCell”。
一旦该文件被创建时,你就能在Project Navigator中找到它。选择“SimpleTableCell.xib”切换到界面生成器。我们要设计的自定义表格单元格的外观。
在对象库中,选择“Table View Cell(表格视图单元格)”,将其拖动到界面生成器的设计区域。
为了容纳更大的缩略图,我们要改变单元格的高度。只要按住下/上侧的单元格边缘并缩放到高度78。
或者,您也可以使用“Size Inspector”改变高度。
表查看单元格的尺寸督察
下一步,选择“Attributes Inspector(属性检查器)“的上半部分的通用区域来为自定义单元格“SimpleTableCell”设置“Identifier(标识符)”。这个标识符将在后面的代码中使用。
表格单元格视图配置好之后,我们将往他里面放其他元素。选择“Image View”,并把它拖到表视图单元格。
image view用于显示缩略图。您可以调整其大小,使其适合单元格。参考数值,我设置的高度和宽度为69像素。
接下来,我们将添加三个标签:Name(姓名),Prep Time(准备时间)和Time(时间)。“Name”标签将用于显示配方的名称。“Prep
Time”是一个静态的标签,只显示“准备时间”。最后,“Time”的标签是一个动态的标签被用于显示实际的具体菜肴的准备时间。
要添加一个标签,在Object library(对象库)中选择“标签”,将其拖动到单元格中。您可以双击标签以更改其名称。
和上面所显示的做比较,您可能会发现您的字体大小和样式是不同的。要更改字体样式,只需选择标签并选择“Attribute Inspector(属性检查器)”。从这里,你可以改变“字体”和最小字体大小的设置。您也可以通过检查更改文本颜色和对齐方式。
您的最终设计应该类似于这样:
创建一个类的自定义单元格
到目前为止,我们已经设计了表格单元格。但如何才能改变自定义单元格的标签值吗?我们要为自定义表视图单元格创建一个新的类。这个类代表了自定义单元格的底层数据模型。
就像以前一样,右键单击“SimpleTable的项目浏览器”文件夹中,然后选择“新建文件”。
选择一个新的文件模板
选择该选项后,Xcode会提示你选择一个模板。我们要创建一个新的自定义表格视图单元格类,选择“Objective-C类”下的“Cocoa
Touch”,然后单击“下一步”。
为您的项目创建新的文件
类名填写“SimpleTableCell”,选中&Subclass of &里的“UITableViewCell”。
单击“下一步”,保存的文件在SimpleTable项目文件夹,并单击“创建”继续。Xcode将在Project Navigator中创建两个名为“SimpleTableCell.h”文件和“SimpleTableCell.m”。
正如前面提到的中,SimpleTableCell类作为自定义单元格的数据模型。在单元格中,我们有三个值是可变的:thumbnail image view(缩略图图像视图),name
label(名称标签)和time label(时间标签)。在这个类里,我们将添加三个属性来表示这些动态值。
打开“SimpleTableCell.h”和之前的”行中添加以下属性:
@property&(nonatomic, weak)&IBOutlet
UILabel&*nameL&&
@property&(nonatomic, weak)&IBOutlet
UILabel&*prepTimeL&&
@property&(nonatomic, weak)&IBOutlet
UIImageView&*thumbnailImageV
Property(属性)和Outlet(出口)
上面的代码定义了三个以后要与在Interface Builder中的表格单元格视图连接的实例变量。使用关键字”在类中声明属性,格式为:
@property (attributes)
参考上面的代码,weak和nonatomic是attributes。UILabel和UIImageView是type,而“nameLabel”,“prepTimeLabel”和“thumbnailImageView”是name。
那么,IBOutlet是什么?你可以把IBOutlet想成一个指令。为了与表视图单元格(即SimpleTableCell.xib)中的元素相关联,我们使用关键字“IBOutlet”让Interface
Builder中知道,他们被允许连接。后来,你会看到如何使这些outlets和在Interface Builder中的对象之间的连接。
现在,打开“SimpleTableCell.m”,以下“@implementation SimpleTableCell”下面添加以下代码:
@synthesize&nameLabel&=&_nameL&&
@synthesize&prepTimeLabel&=&_prepTimeL&&
@synthesize&thumbnailImageView&=&_thumbnailImageV
@synthesize 指令
”关键字告诉编译器自动生成代码来访问我们前面声明的属性。如果你忘了这个指令,Xcode会象下面这样报错:
保存更改并选择“SimpleTableCell.xib”回到Interface Builder中。现在我们可以在类的属性和Label/ ImageView之间创建连接。
首先,选择单元格,在“Identity Inspector”中把类改为“SimpleTableCell”,这样就可以在单元格视图和我们之前创建的类之间建立联系。
现在,我们要建立的连接的属性。右键单击&Objects&下的“SimpleTableCell”,以显示“Outlets”查询器。单击并按住“nameLabel”旁边的圆圈,并将其拖动到Label
– Name”对象。Xcode会自动建立连接。
为“prepTimeLabel”和“thumbnailImageView”重复上述操作:
连接“prepTimeLabel” 和“Label – Time”&连接“thumbnailImageView” 和“ImageView”&
当你建立所有的连接后,它看起来应该是这样的:
更新SimpleTableViewController
我们已经完成了自定义表格单元格的设计和编码。最后,我们来到最后修改的部分 - 确认自定义单元格放在SimpleTableViewController中。
让我们重温在“SimpleTableView.m”的代码:
-&(UITableViewCell&*)tableView:(UITableView&*)tableView
cellForRowAtIndexPath:(&*)indexPath&&
&&&&static&&*simpleTableIdentifier&=&@&SimpleTableItem&;&&
&&& UITableViewCell&*cell&=&[tableView
dequeueReusableCellWithIdentifier:simpleTableIdentifier];&&
&&&&if&(cell&==&nil)&{&
&&&&&&& cell&=&[[UITableViewCell
alloc]&initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];&&
&&& cell.textLabel.text&=&[tableData objectAtIndex:indexPath.row];&&
&&& cell.imageView.image&=&[UIImage imageNamed:[thumbnails
objectAtIndex:indexPath.row]];&&
&&&&return&&&
我们先前使用默认的表视图单元格(即UITableViewCell)显示的表项。为了使用我们的自定义表格单元格,我们必须改变在“SimpleTableView.m”代码:
-&(UITableViewCell&*)tableView:(UITableView&*)tableView
cellForRowAtIndexPath:(&*)indexPath&&
&&&&static&&*simpleTableIdentifier&=&@&SimpleTableCell&;&&
&&& SimpleTableCell&*cell&=&(SimpleTableCell&*)[tableView
dequeueReusableCellWithIdentifier:simpleTableIdentifier];&&
&&&&if&(cell&==&nil)&&
&&&&&&&&&*nib&=&[[&mainBundle]&loadNibNamed:@&SimpleTableCell&&owner:self
options:nil];&&
&&&&&&& cell&=&[nib objectAtIndex:0];&&
&&& cell.nameLabel.text&=&[tableData objectAtIndex:indexPath.row];&&
&&& cell.thumbnailImageView.image&=&[UIImage imageNamed:[thumbnails
objectAtIndex:indexPath.row]];&&
&&& cell.prepTimeLabel.text&=&[prepTime objectAtIndex:indexPath.row];&&
&&&&return&&&
然而,当你你更新代码后,,Xcode检测会有一些错误在源代码编辑器。
源代码的错误指示由Xcode
有什么问题吗?我们刚刚修改的代码告诉“SimpleTableViewController”去使用“SimpleTableCell”类的表格单元格。然而,“SimpleTableViewController”不知道“SimpleTableCell”类。这就是为什么Xcode会显示错误。
正如在第一个教程说的,一个头文件中声明一个类的接口。对于“SimpleTableViewController”要认识“SimpleTableCell”,我们必须在“SimpleTableViewController.m”导入“SimpleTableCell.h”。
导入SimpleTableCell.h
通过导入“SimpleTableCell.h”,“SimpleTableViewController”知道&SimpleTableCell是什么并可以利用它。
最后,由于表格单元格的高度改为78,在”前面添加下面的代码。
-&(CGFloat)tableView:(UITableView&*)tableView
heightForRowAtIndexPath:(&*)indexPath&&
&&&&return&78;&&
现在,点击“Run”按钮和您的SimpleTable的应用程序看起来应该象下面这样:
您可能会注意到应用程序没有显示具体时间。我故意把这个给你。尝试在你的代码做一些修改来更新准备时间。你最终的应用程序看起来非常类似下面的:
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:6517次
排名:千里之外
原创:34篇
转载:23篇
(3)(6)(5)(4)(4)(2)(10)(5)(2)(1)(6)(4)(5)2994人阅读
tableviewcell删除按钮实现
转载请注明本文出自surfaceeee的博客(http://blog.csdn.net/u/article/details/),请尊重他人的辛勤劳动成果,谢谢!
本人一枚新入行的iOS程序猿,苦于tableviewcell左划删除按钮自定义(需要自定义图片)的方法,网上寻找多日未果,遂闭关两日,琢磨得此方法,第一次写博客,各位看官轻拍!废话少说,直入正题。
实现效果如图: & &
注意:此种方法适用于单个按钮功能自定义,但不适用于左划出现多个按钮的自定义(其实是我在赶项目,也没想如何多个按钮= =),而且可以点击任意地方取消删除操作,cell返回原来的位置
本方法实现的原理是将自定义按钮加在tableViewCell.contentView的屏幕外的frame上,打个比方,如果是5系的话,那么你自定义按钮的frame的起点就在(320+,0)(320+表示大于等于320...),当你滑动,整个cell往左偏移的时候,这时候本应该右边显示为这样:
但是由于我们把自定义的按钮add到屏幕外,此时contentView上的自定义按钮就将原删除按钮给遮住了。然后接下来就是实现删除了,重写-(void)tableView:(UITableView
*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath
*)indexPath方法,有了indexPath就可以将cell删除了,然后刷新tableView即可
贴上代码:
覆写的tableViewCell的初始化方法:
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_deleteView=[[UIView alloc]initWithFrame:CGRectMake(kScreenWidth, 0, kScreenWidth, 100)];
_deleteView.backgroundColor=BaseViewBackgroundC
[self.contentView addSubview:_deleteView];
ButtonItem *deleteBtn=[[ButtonItem alloc]initWithFrame:CGRectMake(0, 0, 80, 100) WithImageName:@&icon_delete& WithImageWidth:48 WithImageHeightPercentInItem:.7 WithTitle:NSLocalizedString(@&DeleteOrder&, nil) WithFontSize:14 WithFontColor:[UIColor blackColor] WithGap:-5];
&span style=&white-space:pre&& &/span&//ButtonItem是我自己定义的一个控件,下面有介绍,&span style=&font-family: Arial, Helvetica, sans-&&@&icon_delete&是删除按钮的图片&/span&
[_deleteView addSubview:deleteBtn];
覆写tableView的代理方法:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
[mutableArray removeObjectAtIndex:indexPath.row];
[tableView reloadData];
当然如果只是这样,你会发现你左划完成之后,你的按钮只会有原delete按钮的宽度,此时就可以再通过如下代理方法进行宽度的调节...
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @&通过改变title的长度,你想多宽就多宽&;
ButtonItem是我自己写的一个继承于UIControl的控件,上面有UIImageView和UILable,便于我实现那种带图片和文字的按钮
- (id)initWithFrame:(CGRect)frame WithImageName:(NSString *)imageName WithImageWidth:(CGFloat)imgWidth WithImageHeightPercentInItem:(CGFloat)imgPercent WithTitle:(NSString *)title WithFontSize:(CGFloat)fontSize WithFontColor:(UIColor *)color WithGap:(CGFloat)gap{
self.backgroundColor=[UIColor clearColor];
self=[super initWithFrame:frame];
if (self) {
_imageView=[[UIImageView alloc]initWithFrame:CGRectMake((frame.size.width-imgWidth)/2, 5, imgWidth, imgPercent*frame.size.height)];
if (imageName) {
_imageView.image=[UIImage imageNamed:imageName];
_imageView.contentMode=UIViewContentModeScaleAspectF
[self addSubview:_imageView];
_label=[[UILabel alloc]initWithFrame:CGRectMake(0, gap+_imageView.frame.size.height, frame.size.width, (1-imgPercent)*frame.size.height)];
_label.text=
_label.textColor=
_label.textAlignment=NSTextAlignmentC
_label.font=[UIFont systemFontOfSize:fontSize];
[self addSubview:_label];
至此,自定义tableView删除按钮就全部完成了,其实也就是就原delete按钮进行覆盖,然后根据你要的宽度设置title的长度;点击任意地方取消删除也是直接用的系统的方法。
第一次写博客,语言比较混乱,希望大家谅解。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:3015次
排名:千里之外自定义类继承ImageView 实现多点图片触碰的拖动和缩放 -
- ITeye技术网站
博客分类:
最近的一个android 项目中,客户要求在查看拍照上传的图片时,可以对图片进行多点触控的拖拽、放大、缩小等操作。网上的范例都不怎么好,实现的效果差强人意。
下面说说我的方案:
步骤一:自定义一个View,该View继承自ImageView。在该View中实现多点触控的拖拽、缩放等功能。
步骤二:像使用ImageView那样,在XML中引入该View。当然也可以动态创建该View然后添加到容器组件中。
一、自定义TouchView继承ImageView
package com.hzunitech.xcgk.
import android.content.C
import android.util.AttributeS
import android.util.FloatM
import android.view.MotionE
import android.view.animation.TranslateA
import android.widget.ImageV
* 继承ImageView 实现了多点触碰的拖动和缩放
* @author lihua
public class TouchView extends ImageView {
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
static final int BIGGER = 3;
static final int SMALLER = 4;
private int mode = NONE;
//当前的事件
private float beforeL
//两触点距离
private float afterL
//两触点距离
private float scale = 0.04f;
//缩放的比例 X Y方向都是这个值 越大缩放的越快
private int screenW;
private int screenH;
/*处理拖动 变量 */
private int start_x;
private int start_y;
private int stop_
private int stop_
private TranslateA //处理超出边界的动画
* 默认构造函数
* @param context
public TouchView(Context context){
super(context);
* 该构造方法在静态引入XML文件中是必须的
* @param context
* @param paramAttributeSet
public TouchView(Context context,AttributeSet paramAttributeSet){
super(context,paramAttributeSet);
* 该构造函数在动态创建时,指定图片的初始高宽
* @param context
* @param w
* @param h
public TouchView(Context context,int w,int h) {
super(context);
this.setPadding(0, 0, 0, 0);
* 就算两点间的距离
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
* 处理触碰..
public boolean onTouchEvent(MotionEvent event)
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
mode = DRAG;
stop_x = (int) event.getRawX();
stop_y = (int) event.getRawY();
start_x = (int) event.getX();
start_y = stop_y - this.getTop();
if(event.getPointerCount()==2)
beforeLenght = spacing(event);
case MotionEvent.ACTION_POINTER_DOWN:
if (spacing(event) & 10f) {
mode = ZOOM;
beforeLenght = spacing(event);
case MotionEvent.ACTION_UP:
/*判断是否超出范围
int disX = 0;
int disY = 0;
if(getHeight()&=screenH || this.getTop()&0)
if(this.getTop()&0 )
int dis = getTop();
this.layout(this.getLeft(), 0, this.getRight(), 0 + this.getHeight());
disY = dis - getTop();
else if(this.getBottom()&screenH)
disY = getHeight()- screenH+getTop();
this.layout(this.getLeft(), screenH-getHeight(), this.getRight(), screenH);
if(getWidth()&=screenW)
if(this.getLeft()&0)
disX = getLeft();
this.layout(0, this.getTop(), 0+getWidth(), this.getBottom());
else if(this.getRight()&screenW)
disX = getWidth()-screenW+getLeft();
this.layout(screenW-getWidth(), this.getTop(), screenW, this.getBottom());
if(disX!=0 || disY!=0)
trans = new TranslateAnimation(disX, 0, disY, 0);
trans.setDuration(500);
this.startAnimation(trans);
mode = NONE;
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
case MotionEvent.ACTION_MOVE:
/*处理拖动*/
if (mode == DRAG) {
if(Math.abs(stop_x-start_x-getLeft())&88 && Math.abs(stop_y - start_y-getTop())&85)
this.setPosition(stop_x - start_x, stop_y - start_y, stop_x + this.getWidth() - start_x, stop_y - start_y + this.getHeight());
stop_x = (int) event.getRawX();
stop_y = (int) event.getRawY();
/*处理缩放*/
else if (mode == ZOOM) {
if(spacing(event)&10f)
afterLenght = spacing(event);
float gapLenght = afterLenght - beforeL
if(gapLenght == 0) {
else if(Math.abs(gapLenght)&5f)
if(gapLenght&0) {
this.setScale(scale,BIGGER);
this.setScale(scale,SMALLER);
beforeLenght = afterL
* 实现处理缩放
private void setScale(float temp,int flag) {
if(flag==BIGGER) {
this.setFrame(this.getLeft()-(int)(temp*this.getWidth()),
this.getTop()-(int)(temp*this.getHeight()),
this.getRight()+(int)(temp*this.getWidth()),
this.getBottom()+(int)(temp*this.getHeight()));
}else if(flag==SMALLER){
this.setFrame(this.getLeft()+(int)(temp*this.getWidth()),
this.getTop()+(int)(temp*this.getHeight()),
this.getRight()-(int)(temp*this.getWidth()),
this.getBottom()-(int)(temp*this.getHeight()));
* 实现处理拖动
private void setPosition(int left,int top,int right,int bottom) {
this.layout(left,top,right,bottom);
二、在XML文件中引入TouchView。
在xml中引入TouchView和引入其父类ImageView是一样的。
&ImageView
android:id="@+id/drawing"
android:layout_width="fill_parent"
android:layout_height="fill_parent" &
&/ImageView&
&com.hzunitech.xcgk.ui.TouchView
android:id="@+id/drawing"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="50px"
android:gravity="center"&
&/com.hzunitech.xcgk.ui.TouchView&
在Activity中,为找到该组件,并为其动态设置图片。
TouchView drawing = (TouchView)findViewById(R.id.drawing);
//獲得網絡url上的一個圖片
Bitmap img = AvdUtils.getHttpBitmap(Constants.URL_PREFIX+takan.getFileName());
drawing.setImageBitmap(img);
浏览 12758
浏览: 239314 次
来自: 杭州

我要回帖

更多关于 cell嵌套tableview 的文章

 

随机推荐