Objective c Xcode:如何在UIScrollView上单击图像
我正在尝试使UIScrollView具有图像列表,这些图像在单击后将切换到不同的图像。本例是模拟选择图像 我尝试使用UIButton而不是UIImage,切换图像非常容易,但当我右键单击按钮时无法滚动它们。只有当我在按钮之间单击时,它才滚动 然后我尝试使用UIImages,它很容易滚动,但单击后我无法切换图像 我稍微挖掘了一下,发现了以下帖子: 这似乎是我需要的东西,但我做错了什么 我的代码:Objective c Xcode:如何在UIScrollView上单击图像,objective-c,xcode,cocoa,uiscrollview,uiimageview,Objective C,Xcode,Cocoa,Uiscrollview,Uiimageview,我正在尝试使UIScrollView具有图像列表,这些图像在单击后将切换到不同的图像。本例是模拟选择图像 我尝试使用UIButton而不是UIImage,切换图像非常容易,但当我右键单击按钮时无法滚动它们。只有当我在按钮之间单击时,它才滚动 然后我尝试使用UIImages,它很容易滚动,但单击后我无法切换图像 我稍微挖掘了一下,发现了以下帖子: 这似乎是我需要的东西,但我做错了什么 我的代码: //TouchSimpleView.h #import <UIKit/UIKit.h>
//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
//TouchImpleView.h
#进口
@界面TouchSimpleView:UIImageView{
id代表;
}
@属性(保留)id委托;
@结束
@接口NSObject(TouchSimpleView)
-(无效)didTouchView:(UIView*)aView;
@结束
//TouchSimpleView.m
#导入“TouchSimpleView.h”
@ImpleView的实现
@综合代表;
-(id)initWithFrame:(CGRect)帧
{
self=[super initWithFrame:frame];
如果(自我){
//初始化代码
}
回归自我;
}
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件{
NSLog(@“touchesBagan”);
if(delegate!=nil&&[delegate respondsToSelector:@selector(didTouchView:)]{
[代理视图:self];
}
[超级触摸开始:触摸事件:事件];
}
@结束
我是这样使用上面的:
// 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
//ViewController.h
#进口
#导入“TouchSimpleView.h”
@界面ViewController:UIViewController{
TouchSimpleView*touchView;
UIImageView*银行ImageView;
}
@属性(非原子,保留)TouchImpleView*touchView;
@属性(非原子,保留)UIImageView*bankImageView;
@结束
//ViewController.m
#导入“ViewController.h”
@界面视图控制器()
@结束
@实现视图控制器
@综合触摸视图;
@综合图像视图;
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
touchView=[[TouchImpleView alloc]initWithFrame:CGRectMake(0,0,100,100)];
touchView.delegate=self;
[self.view addSubview:touchView];
bankImageView=[[UIImageView alloc]initWithFrame:touchView.frame];
bankImageView.image=[UIImage ImageName:@“1.jpg”];
[触摸视图添加子视图:bankImageView];
}
-(无效)didTouchView:(UIView*)查看{
NSLog(@“触摸视图;更改图像”);
}
@结束
没有我在开始时提到的UIScrollView,但我想让这个简单的示例起作用,然后将touchView添加到UIScrollView中
我是iOS编程的初学者。
请告知。确保
UIImageView
属性userInteractionEnabled
设置为YES
默认值为
否
确保UIImageView
属性userInteractionEnabled
设置为YES
默认值为
否
解决方案是:
- (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;
touchView.userInteractionEnabled = YES;
touchView.image = [UIImage imageNamed:@"1.jpg"];
[self.view addSubview:touchView];
}
谢谢@Mundi解决方案是:
- (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;
touchView.userInteractionEnabled = YES;
touchView.image = [UIImage imageNamed:@"1.jpg"];
[self.view addSubview:touchView];
}
谢谢@Mundi我坐过bankImageView。userInteractionEnabled=是,没有帮助。bankImageView不是一个子类图像视图。您只在您的
touchImpleView
课程中实现了touchsbegind
。谢谢@Mundi的回答,但我是iOS新手,我不太明白需要修复什么。我知道什么是继承,但不知道如何在iOS中实现它。你能给我提供一些示例代码作为答案吗?只需将类TouchSimpleView的bankImageView改为UIImageView,它就应该可以工作了。我已设置bankImageView.userInteractionEnabled=是,没有帮助。bankImageView不是一个子类图像视图。您只在您的touchImpleView
课程中实现了touchsbegind
。谢谢@Mundi的回答,但我是iOS新手,我不太明白需要修复什么。我知道什么是继承,但不知道如何在iOS中实现它。您能给我提供一些示例代码作为答案吗?只需将TouchImpleView类的bankImageView改为UIImageView,它就可以工作了。