1

我刚刚开始从事一个项目,我已经遇到了一些问题和错误。我想知道你们是否可以帮助我找出错误在哪里。

快速概览:我有两个文件:(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] 出来的图像位于滚动条的顶部. 这就是我想要的层次结构。

非常感谢你。

4

2 回答 2

2

您不必为按钮按下编写自己的触摸处理例程。而不是使用UIImageViews,只需创建UIButtons 并挂钩到他们的UIControlEventTouchUpInside事件。

于 2012-08-31T20:49:33.080 回答
0

我认为您最好告诉我们您要完成的工作,因为您可能在这里找错了树。正如 Jim 所说,有更好的选择图像的方法,即使您想自己处理触摸,UIGestureRecognizers 也可能是更好的选择。如果您允许用户选择图像,我建议您查看 github 上的 iCarousel。这是一个开源轮播,非常适合图像选择。此外,如果您可以针对 iOS 6,则有一个新的集合视图可能就在您的小巷里。

于 2012-08-31T20:58:42.753 回答