我有一个带有子视图UIView
UIScrollView
,这里UIView
也有类型为 UIImageView
的子视图。
层次结构类似于ViewController -> UIScrollView -> UIView -> UIImageView
.
所有,UIScrollView, UIView, UIImageView
动态添加的,所有启用userInteractionEnabled=YES
,当用户滚动下一个 SIX UIViews
时,UIScrollView
上立即可见 SIX 视图。
我正在实施 UITouch 事件来检测UIImageView
上的任何触摸
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//I want to detect particular UIImageView
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//Here, user can move any UIImageView
}
但是,我无法检测到触摸。我尝试这里所说的其他一些方式。还尝试了一些我的想法建议我,但我发现并意识到UIScrollView
下的任何子视图都无法检测到任何对象。
我还尝试了另一件事,我在self.view
中添加了一个UIView
作为子视图,任何有趣的事情,它的检测touch
!
有没有遇到这种问题的人,有什么解决方案?任何帮助将不胜感激。
试试这个:-
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
//sampleImageView is the imageView.
CGPoint location = [touch locationInView:sampleImageView];
if ([touch view] == sampleImageView)
{
//.....
}
}
将其用于触摸检测。检查它 希望对您有所帮助。谢谢:)
你可以做一件事,
同样的问题我已经面临过,所以在UIscrollview中添加Caustom UIButton并设置图像并定义标记所有按钮到UIButton中。这种方式工作非常顺利。
UIScrollView* scrForSubItem = [[UIScrollView alloc] initWithFrame:CGRectMake(10,2,1024,580)];
[scrForSubItem setAlwaysBounceVertical:YES];
scrForSubItem.minimumZoomScale = 1;
scrForSubItem.maximumZoomScale = 3;
scrForSubItem.scrollEnabled = YES;
scrForSubItem.pagingEnabled = NO;
scrForSubItem.directionalLockEnabled = YES;
scrForSubItem.showsVerticalScrollIndicator = YES;
scrForSubItem.backgroundColor = [UIColor clearColor];
scrForSubItem.delegate = self;
int xOffset = 0;
int yOffset = 0;
for (int i = 0; i < [currnetitem count]; i++) {
UIImageView* imgViewBtn=[[[UIImageView alloc]initWithFrame:CGRectMake(xOffset,yOffset, 246,170)] autorelease];
[imgViewBtn setUserInteractionEnabled:YES];
[imgViewBtn setContentMode:UIViewContentModeScaleAspectFit];
imgViewBtn.image=btnImg;
UIButton *btnSelectTile = [UIButton buttonWithType:UIButtonTypeCustom];
[btnSelectTile setFrame:CGRectMake(0,0,246,170)];
[btnSelectTile setTag:i];
[btnSelectTile setBackgroundColor:[UIColor clearColor]];
[btnSelectTile addTarget:self action:@selector(scrImagePressed:) forControlEvents:UIControlEventTouchUpInside];
[imgViewBtn addSubview:btnSelectTile];
[self.scrForSubItem addSubview:imgViewBtn];
xOffset += btnSelectTile.frame.size.width+10;
if(xOffset>scrollWidth)
{
xOffset=0;
yOffset += btnSelectTile.frame.size.height+10;
[scrForSubItem setContentSize:CGSizeMake(xOffset,yOffset)];
}
else{
[scrForSubItem setContentSize:CGSizeMake(xOffset,170)];
}
[self.viewa ddSubview:scrForSubItem];
}
//创建 .h 文件
@interface AppScrollView : UIScrollView {
}
- (id)initWithFrame:(CGRect)frame;
@end
创建 .m 文件
导入"AppScrollView.h"
@implementation 应用滚动视图
-
(id)initWithFrame:(CGRect)frame{if ([super initWithFrame:frame]){}回报自我;}
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event{
如果不拖动,则将事件发送给下一个响应者
如果(!self.dragging)
[self.nextResponder touchesEnded: touches withEvent:event];
还
[super touchesEnded: touches withEvent: event];
}
@end
现在在你的UIView控制器中
创建用于滚动视图的对象
应用滚动视图* 滚动视图;