【问题标题】:Buttons get stuck on highlighted按钮卡在突出显示
【发布时间】:2012-08-31 20:39:19
【问题描述】:

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

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

非常感谢。

【问题讨论】:

    标签: ios cocoa-touch ios5 uiscrollview touchesbegan


    【解决方案1】:

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

    【讨论】:

    • 那么,你认为 UIButton obj 会更好吗?而不是 UIImageViews?我正在尝试制作的按钮是自定义的 PNG。
    • 是的,使用按钮,这正是它们的设计目的。
    【解决方案2】:

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

    【讨论】:

    • 我的基本想法是在 addImagesToView 函数中创建一个带有背景、滚动条和滚动条上的一些图像的视图。这些图像是我在 Photoshop 中制作的两个版本的按钮:正常和突出显示。你能告诉我更多关于 iOS 6 的集合视图的信息吗?谢谢。
    猜你喜欢
    • 1970-01-01
    • 2015-09-27
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    相关资源
    最近更新 更多