Xcode:如何在图像触摸时更改图像(相同位置)? [英] Xcode: How to change Image on Touching at an Image (same Position)?

查看:138
本文介绍了Xcode:如何在图像触摸时更改图像(相同位置)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单地说,我已经在UIView中使用Interface Builder放置了一个图像。现在我想在触摸该图像时运行一个方法/函数,例如当应该加载另一个图像时。

Simply, I have placed an image with the Interface Builder in a UIView. Now I want to run a method/function when I am touching that image, for example when another image should be loaded.

我的代码:

- (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:方法的实现,这里最简单的例子:

You need handle touch event from you UIView. To do it you should create subclass of UIView and add your realisation of touchesBegan:withEvent: method, here simplest example:

//  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

然后你可以使用这个类的视图想要处理触摸,例如:

Then you can use views of this class when you want to handle touches, for example:

- (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"];
}

这篇关于Xcode:如何在图像触摸时更改图像(相同位置)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆