gpt4 book ai didi

iphone - UIScrollView 触摸和监听事件

转载 作者:行者123 更新时间:2023-11-29 13:31:43 26 4
gpt4 key购买 nike

我在 UIScrollView 中有一个图像,我有代码在用户拖动手指时绘制文本。我希望能够控制,当用户触摸屏幕时,如果

a) 用户应该用他们的手指画画或者

b) 用户应该用手指移动 ScrollView

我有一个 bool 值,它跟踪用户正在做什么。我有触摸方法:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
if(draw == false) {
printf("calling super");
[scrollView touchesBegan:touches withEvent:event];
}
else
[myPath moveToPoint:[mytouch locationInView:self]];

}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
if(draw == false)
[scrollView touchesMoved:touches withEvent:event];
else {
[myPath addLineToPoint:[mytouch locationInView:self]];
[self setNeedsDisplay];
}

为什么这行不通?基本上,如果我不绘图,我会调用 ScrollView 的 touchesBegan 和 touchesMoved。如果我正在绘图,我会使用 myPath 进行绘图。

但是,当 draw 为 false 时, ScrollView 不会四处移动或放大等。

最佳答案

我以前遇到过这个问题。我这样解决:

你应该制作一个 mainView 。它有2个属性。一个是 yourScrollView ,一个是 yourDrawImageView。 [yourScrollView addSubviews:yourDrawImageView];[mainView addSubviews:yourScrollView];

然后像这样在 mainView.m 中编写你的 touches 方法(忽略 scrollView 语句)

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
if ([[touches allObjects] isKindOfClass:[yourDrawImageView class]])
{

[myPath moveToPoint:[mytouch locationInView:self]];
}

}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
if ([[touches allObjects] isKindOfClass:[yourDrawImageView class]])
{

[myPath addLineToPoint:[mytouch locationInView:self]];
[self setNeedsDisplay];
}


}

最后一步,你忽略的是编写 scrollView touches 事件,它写在你的 SceollView.m 中,像这样:

#import "yourScrollView.h"


@implementation yourScrollView


- (id)initWithFrame:(CGRect)frame {

self = [super initWithFrame:frame];
if (self) {
// Initialization code.
}
return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesBegan:touches withEvent:event];
if(!self.dragging)
[[self nextResponder] touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesMoved:touches withEvent:event];
if(!self.dragging)
[[self nextResponder] touchesMoved:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesEnded:touches withEvent:event];
if(!self.dragging)
[[self nextResponder] touchesEnded:touches withEvent:event];
}

- (void)dealloc {
[super dealloc];
}


@end

希望对您有所帮助:-)

关于iphone - UIScrollView 触摸和监听事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11694208/

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