我有UIScrollView
个UIImageViews
。我需要将图片从UIScrollView
拖放到另一个视图。在scrollView touch之外工作。但内部滚动视图触摸不起作用。我使用了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 !!");
}
}
答案 0 :(得分:4)
您需要使用UIImageView
自己的视图继承来自定义UIImageView
。在该自定义子类中提供触摸方法,并将其添加到UIScrollView
..
UIScrollview
的子视图永远不会直接调用touchesBegan
方法。您需要使用子视图进行自定义,以获取添加的子视图/自定义视图的touchesBegan
属性。
我的意思是说像ImageView的子类一样
CustomImageView *imageView = [[CustomImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
[imageView setUserInteractionEnabled:YES];
[scrollView addSubview:imageView];
[imageView release];
CustomImageView
类应该从UIImageView继承,如
@interface CustomImageView : UIImageView
{
}
<。>文件中的
#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;
}
}
答案 1 :(得分:3)
在滚动视图中添加Tap手势识别器,以便在滚动视图内启用触摸:
UITapGestureRecognizer *singlTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(methodName:)];
singlTap.cancelsTouchesInView = NO; //Default value for cancelsTouchesInView is YES, which will prevent buttons to be clicked
[scrollViewName addGestureRecognizer:singlTap];
然后用指定的方法编写代码。
-(void)methodName:(UITapGestureRecognizer *)gesture
{
//your code here
}
答案 2 :(得分:1)
我不知道这对你有帮助吗。但请尝试将exclusiveTouch
的{{1}}属性设置为UIImageView
。
答案 3 :(得分:0)
您是否将滚动视图添加为IBOutlet?如果它来自xib并且如你所说的触摸可以在滚动之外而不是在内部,我猜你可能不小心取消选中阻止触摸事件的scrollview的 userInteractionEnabled 。并且您已将UIImageViews添加为UIScrollView的子视图。对于UIImageView,您必须先将 userInteractionEnabled 设置为 YES ,然后根据需要添加任何手势以获取事件或使用 touchesBegan,touchesMoved 方法。除非您启用了交互功能,否则您将无法在UIImageView上接收触摸事件。如果父视图我的意思是UIScrollView的交互被禁用,你也不会接触到UIImageView。希望这会有所帮助:)