gpt4 book ai didi

ios - UIScrollView 中 subview 的 touchEvent

转载 作者:行者123 更新时间:2023-11-29 03:16:15 24 4
gpt4 key购买 nike

全部,

我有一个问题,我已经在 StackOverflow(和其他互联网)上查看了所有可能的答案,但我尝试过的所有解决方案都没有任何效果......

我有一个 UIScrollView 和一个 UIImageView 作为 subview 来创建一个板。我可以放大和缩小,效果很好。滚动也很好!

现在,我想将其他 UIImageView(图 block )拖放到 ScrollView 中,并拖放到 ScrollView 之外。但是当我将 UIImageView 放到 ScrollView 中时,我无法拖放 subview 。

我在viewDidLoad中设置了UIScrollView的setCanCancelContentTouches:NO。
我创建了一个子类 TileImageView,并在初始化中将 exclusiveTouch 设置为 YES。
注意:boardImage 是“普通”UIImageView,tile 属于子类!

补充: 当我将 boardImage 的 UserInteraction 设置设置为 NO 时,ScrollView 缩放但未记录 touchBegan。如果我将其设置为 YES,则 ScrollView 不会缩放,但会记录 touchBegan..

所以我保留它以便缩放,但是 Tiles 一旦进入 ScrollView 就不能再拖动了。
我究竟做错了什么?或者我错过了什么?

PS:设置 delaysContentTouches 属性会使滚动不活动,所以这也不是解决方案...

代码:RootViewController_iphone.m

@implementation RootViewController_iphone

@synthesize boardScrollView;
@synthesize dragObject;
@synthesize boardImage;

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

UIImage *image = [UIImage imageNamed:@"greyblue_numbered_15x15_900x900.png"];
self.boardImage = [[UIImageView alloc] initWithImage:image];
self.boardImage.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size};
[self.boardScrollView addSubview:self.boardImage];

self.boardImage.userInteractionEnabled = YES;

self.boardScrollView.contentSize = image.size;
self.boardScrollView.userInteractionEnabled = YES;
self.boardScrollView.canCancelContentTouches = NO;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([touches count] == 1) {
// one finger

CGPoint touchPoint = [[touches anyObject] locationInView:self.view];

for (UIImageView *iView in self.view.subviews) {
if ([iView isMemberOfClass:[TileImageView class]]) {
if (touchPoint.x > iView.frame.origin.x &&
touchPoint.x < iView.frame.origin.x + iView.frame.size.width &&
touchPoint.y > iView.frame.origin.y &&
touchPoint.y < iView.frame.origin.y + iView.frame.size.height)
{
self.dragObject = iView;
self.touchOffset = CGPointMake(touchPoint.x - iView.frame.origin.x,
touchPoint.y - iView.frame.origin.y);
self.homePosition = CGPointMake(iView.frame.origin.x,
iView.frame.origin.y);
[self.view bringSubviewToFront:self.dragObject];
}
}
}
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self.view];

CGRect newDragObjectFrame = CGRectMake(touchPoint.x - touchOffset.x,
touchPoint.y - touchOffset.y,
self.dragObject.frame.size.width,
self.dragObject.frame.size.height);
self.dragObject.frame = newDragObjectFrame;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
NSString *tmp = touch.view.description;

CGPoint touchPointScreen = [[touches anyObject] locationInView:self.view];
CGPoint touchPointImage = [[touches anyObject] locationInView:self.boardImage];
CGPoint touchPointBoard = [[touches anyObject] locationInView:self.boardScrollView];

if (touchPointScreen.x > self.boardScrollView.frame.origin.x &&
touchPointScreen.x < self.boardScrollView.frame.origin.x + self.boardScrollView.frame.size.width &&
touchPointScreen.y > self.boardScrollView.frame.origin.y &&
touchPointScreen.y < self.boardScrollView.frame.origin.y + self.boardScrollView.frame.size.height)
{
self.dragObject.frame = CGRectMake(touchPointImage.x - touchOffset.x,
touchPointImage.y - touchOffset.y,
self.dragObject.frame.size.width,
self.dragObject.frame.size.height);

[self.boardImage addSubview:self.dragObject]; // add to image


}
}

代码:TileImageView.m

@implementation TileImageView:

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.exclusiveTouch = YES;
}
return self;
}

最佳答案

您已经有了 UIImageView 的子类,因此您可以在子类中执行以下操作:
1-覆盖touchsBegan、didEnd和didMove
2-在这些方法中将它们传递给 super View ,即 UIScrollView: [self.superview touchsBegan .... ]
3-如果您的 UIImageView 没有收到这些消息,则在 init 添加 self.userInteractionEnabled = YES;希望这对你有帮助

关于ios - UIScrollView 中 subview 的 touchEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21676227/

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