我正在尝试使用图像列表制作 UIScrollView,这些图像在单击后将切换到不同的图像。案例是模拟选择图像。
我尝试使用 UIButton 而不是 UIImage 并且切换图像非常容易,但是当我单击按钮时无法滚动它们。只有当我在按钮之间单击时它才会滚动。
比我尝试使用 UIImages 并且它很容易滚动,但是单击后我无法切换图像。
我挖了一下,发现了以下帖子:Xcode:如何在触摸图像时更改图像(相同位置)?
这似乎是我需要的东西,但我做错了什么。
我的代码:
//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 编程的初学者。请指教。