gpt4 book ai didi

objective-c - Xcode:如何单击 UIScrollView 上的图像

转载 作者:行者123 更新时间:2023-12-03 17:13:52 24 4
gpt4 key购买 nike

我正在尝试使用图像列表制作 UIScrollView,单击后将切换到不同的图像。本案例是模拟选择一张图片。

我尝试使用 UIButton 而不是 UIImage,切换图像非常容易,但是当我右键单击按钮时无法滚动它们。仅当我在按钮之间单击时它才会滚动。

我尝试使用 UIImages,它很容易滚动,但点击后无法切换图像。

我挖掘了一下,发现了以下帖子:Xcode: How to change Image on Touching at an Image (same Position)?

这似乎是我需要的东西,但我做错了。

我的代码:

//TouchSimpleView.h
#import <UIKit/UIKit.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;

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"touchesBagan");
if(delegate != nil && [delegate respondsToSelector:@selector(didTouchView:)]) {
[delegate didTouchView:self];
}
[super touchesBegan:touches withEvent:event];
}
@end

我正在这样使用上面:

//  ViewController.h
#import <UIKit/UIKit.h>
#import "TouchSimpleView.h"

@interface ViewController : UIViewController {
TouchSimpleView *touchView;
UIImageView *bankImageView;
}

@property (nonatomic, retain) TouchSimpleView *touchView;
@property (nonatomic, retain) UIImageView *bankImageView;
@end


// ViewController.m
#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController
@synthesize touchView;
@synthesize bankImageView;

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

touchView = [[TouchSimpleView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
touchView.delegate = self;
[self.view addSubview:touchView];

bankImageView = [[UIImageView alloc] initWithFrame:touchView.frame];
bankImageView.image = [UIImage imageNamed:@"1.jpg"];
[touchView addSubview: bankImageView];
}

- (void)didTouchView:(UIView *)aView {
NSLog(@"view touched; changing image");
}

@end

我在开始时没有提到 UIScrollView,但我想让这个简单的示例正常工作,并将 touchView 添加到 UIScrollView。

我是 iOS 编程的初学者。请指教。

最佳答案

确保 UIImageView 属性 userInteractionEnabled 设置为 YES
默认为NO

关于objective-c - Xcode:如何单击 UIScrollView 上的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13038279/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com