gpt4 book ai didi

iphone - 检测添加为 UIScrollView subview 的 UIImage 触摸

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:59:25 25 4
gpt4 key购买 nike

我有 UIScrollView 和许多 UIImageViews。我需要将图像从 UIScrollView 拖放到另一个 View 。在 scrollView 之外触摸是有效的。但是在 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 !!");
}

}

最佳答案

您需要使用您自己的继承 UIImageView 的 View 自定义UIImageView。在该自定义子类中提供触摸方法并将其添加到您的 UIScrollView..

UIScrollview 的 subview 永远不会直接调用touchesBegan 方法。您需要使用 subview 进行自定义以获取添加的 subview /自定义 View 的 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
{
}

在#.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;
}

}

关于iphone - 检测添加为 UIScrollView subview 的 UIImage 触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15082309/

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