我刚刚开始从事一个项目,我已经遇到了一些问题和错误。我想知道你们是否可以帮助我找出错误在哪里。
快速概览:我有两个文件:(ViewController.m 和 Scroller.m)。在 ViewController 中,我在 ViewDidLoad 下有一些代码(我将在稍后显示),我还有一个函数 (addImagesToView) 可以执行它所说的操作,并且一些 touchesBegan、moved 和 end 难以处理。
在 Scroller 中,我决定重写一些“-(void)touches”函数实现。
我遇到的问题 是:按钮(来自 addImagesToView 函数)卡在突出显示的位置。我使用 NSLog 进行了一些测试,以查看哪些“触摸”有效,哪些无效。“touchesMoved”不能正常工作。如果用户向下拖动,日志会在大约 3 或 4 行文本后停止。我认为它以某种方式干扰了滚动。
这是 ViewController.m 的内容:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bgPNG.png"]];
imageView.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:imageView];
//UIColor *background = [[UIColor alloc]initWithPatternImage:imageView.image];
//self.view.backgroundColor = background;
CGRect fullScreen = [[UIScreen mainScreen] applicationFrame];
scroll = [[Scroller alloc] initWithFrame:fullScreen];
scroll.contentSize = CGSizeMake(320, 2730);
scroll.delaysContentTouches = NO;
//scroll.canCancelContentTouches = NO;
scroll.scrollEnabled = YES;
[self.view addSubview:scroll];
buttons = [[NSMutableArray alloc]init];
[self addImagesToView];
}
-(void) addImagesToView{
CGFloat yCoordinate = 35;
for (int i=1; i<=18; i++) {
UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"picture%d.png",i]]highlightedImage:[UIImage imageNamed:[NSString stringWithFormat:@"picture%dHG.png",i]]];
CGRect position = CGRectMake(105, yCoordinate, IMAGE_SIZE, IMAGE_SIZE);
image.frame = position;
[scroll addSubview:image];
image.userInteractionEnabled = YES;
image.tag = i;
[buttons addObject:image];
yCoordinate += 150;
}
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Get any touch
UITouch *t = [touches anyObject];
if ([t.view class] == [UIImageView class])
{
// Get the tappedImageView
UIImageView *tappedImageView = (UIImageView*) t.view;
tappedImageView.highlighted = YES;
}
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *t = [touches anyObject];
if ([t.view class] == [UIImageView class])
{
// Get the tappedImageView
UIImageView *tappedImageView = (UIImageView*) t.view;
tappedImageView.highlighted = NO;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources th at can be recreated.
}
@end
这是 Scroller.m
#import "Scroller.h"
@implementation Scroller
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (!self.dragging)
[self.nextResponder touchesBegan: touches withEvent:event];
else
[super touchesBegan: touches withEvent: event];
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *t = [touches anyObject];
if ([t.view class] == [UIImageView class])
{
// Get the tappedImageView
UIImageView *tappedImageView = (UIImageView*) t.view;
tappedImageView.highlighted = NO;
}
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (!self.dragging)
[self.nextResponder touchesEnded: touches withEvent:event];
else
[super touchesEnded: touches withEvent: event]; // Get the tappedImageView
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
我也尝试在 ViewController 中实现所有的触摸,但我不知道如何让它从滚动中读取(请注意 SCROLL 和 SCROLLER 之间的差异)。
正如你所知道的,我尝试像这样对它们进行排序:view -> backGroundImage ->scroller ->addImagesToView(function),以便从 [I]addImagesToView[/I] 出来的图像位于滚动条的顶部. 这就是我想要的层次结构。
非常感谢你。