开发者

Xcode: How to change Image on Touching at an Image (same Position)?

开发者 https://www.devze.com 2022-12-23 04:40 出处:网络
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开发者_Python百科 image, for example when another image should be load

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开发者_Python百科 image, for example when another image should be loaded.

My Code:

- (void)touchesended:(NSSet*) touches withEvent:(UIEvent *)event 
{
    // UITouch *touch = [touch anyObject];
    bild_1_1.image = [UIImage imageNamed:@"t_1_4.jpg"];
}


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


What you want could be easily done by placing UIButton instead of UIImage and changing its background image using method setBackgroundImage:forState: at TouchUpInside event handler.


another solution:

in the .h file

place a IBOutlet UIImagView *imageButton;

and

-(IBAction)doSomething:(id)sender;

in the .m file

-(IBAction)dosomething:(id)sender {

    // your code

   [imageButton setImage:[UIImage imageNamed:@""t_1_4.jpg"} forState:UIControlStateNormal];
}

works at my code very fine


it works with me without creating subclass, in .h file you add UIImageView

#import <UIKit/UIKit.h>
@interface YourClassName : UIViewController{
    IBOutlet UIImageView *wallpaper;
}
@property (nonatomic, retain) IBOutlet UIImageView *wallpaper;
@end

then in .m you can use your method

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [wallpaper setImage:[UIImage imageNamed: @"image.png"]];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消