我有很多UIScrollView的UIImageViews.I需要拖放图像从UIScrollView到另一个视图。在外部的scrollView触摸是有效的。但是里面的滚动视图触摸是不起作用的。我使用了touchesBegan,touchesMoved等方法。请帮帮我。
-(IBAction)selectBut:(id)sender
{
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(x,y,w,h)];
scrollView.userInteractionEnabled = YES;
int y = 0;
for (int i = 0; i < [myArray count]; i++) {
UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, y, 75, 30)];
image.userInteractionEnabled = YES;
y=y+35;
[scrollView addSubview:image];
}
[self.view addSubview:scrollView];
[scrollView setContentSize:CGSizeMake(150, 300)]
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 1) {
NSLog(@"One touch !!");
}
}发布于 2013-02-26 14:03:21
您需要使用您自己的视图来自定义UIImageView 继承自UIImageView的。在定制的子类中提供触摸方法,并将其添加到您的UIScrollView中。
UIScrollview的子视图永远不会直接调用touchesBegan方法。您需要使用subview进行自定义,以获取添加的子视图/自定义视图的touchesBegan属性。
我的意思是以ImageView的子类为例
CustomImageView *imageView = [[CustomImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
[imageView setUserInteractionEnabled:YES];
[scrollView addSubview:imageView];
[imageView release];应该从UIImageView继承CustomImageView类,如下所示
@interface CustomImageView : UIImageView
{
}在# .m文件中
#import "CustomImageView.h"
@implementation CustomImageView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"%s", __FUNCTION__);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2) {
drawImageView.image = nil;
return;
}
}https://stackoverflow.com/questions/15082309
复制相似问题