objective-c – Xcode:如何在图像触摸时更改图像(相同位置)?
发布时间:2020-12-15 01:42:54 所属栏目:百科 来源:网络整理
导读:简单地说,我已经在UIView中使用Interface Builder放置了一个图像.现在我想在触摸该图像时运行方法/功能,例如当应该加载另一个图像时. 我的代码: - (void)touchesended:(NSSet*) touches withEvent:(UIEvent *)event { // UITouch *touch = [touch anyObject
简单地说,我已经在UIView中使用Interface Builder放置了一个图像.现在我想在触摸该图像时运行方法/功能,例如当应该加载另一个图像时.
我的代码: - (void)touchesended:(NSSet*) touches withEvent:(UIEvent *)event { // UITouch *touch = [touch anyObject]; bild_1_1.image = [UIImage imageNamed:@"t_1_4.jpg"]; } 解决方法
您需要处理来自UIView的触摸事件.要做到这一点,你应该创建UIView的子类并添加你的touchesBegan:withEvent:方法的实现,这里最简单的例子:
// TouchSimpleView.h @interface TouchSimpleView : UIImageView { id delegate; } @property(retain) id delegate; @end @interface NSObject(TouchSimpleView) -(void)didTouchView:(UIView *)aView; @end // TouchSimpleView.m #import "TouchSimpleView.h" @implementation TouchSimpleView @synthesize delegate; - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"touchesBegan!!! "); if ( delegate != nil && [delegate respondsToSelector:@selector(didTouchView:)] ) { [delegate didTouchView:self]; } [super touchesBegan:touches withEvent:event]; } @end 然后,当您想要处理触摸时,可以使用此类的视图,例如: - (void)applicationDidFinishLaunching:(UIApplication *)application { [window makeKeyAndVisible]; touchView = [[TouchSimpleView alloc] initWithFrame:CGRectMake(50,50,200,300)]; touchView.delegate = self; [window addSubview:touchView]; imageView =[[UIImageView alloc] initWithFrame:touchView.frame]; imageView.image = [UIImage imageNamed:@"image1.png"]; [touchView addSubview:imageView]; } -(void)didTouchView:(UIView *)aView{ NSLog(@"view touched,changing image"); imageView.image = [UIImage imageNamed:@"image2.png"]; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Oracle修改内存大小
- ruby – 为什么`BasicObject`回答方法`superclass`和`class
- uboot bootargs bootcmd bootm
- Oracle RAC学习笔记01-集群理论_0
- reactjs – React – fetch-intercept修改所有头文件
- oracle 把一张表中的某字段的多条记录用逗号连接
- PostgreSQL直接查询CSV文件的方法:file_fdw
- cocos2dx移植android平台-我的血泪史
- objective-c – 在什么情况下,在Cocoa的try / catch / fina
- swift 字符串,数组,字典 的那些事<二>