ios - 按钮卡在突出显示

标签 ios cocoa-touch ios5 uiscrollview touchesbegan

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

快速概览:我有两个文件:(ViewController.m 和 Scroller.m)。在 ViewController 中,我在 ViewDidLoad 下有一些代码(我将在一秒钟内显示),我还有一个函数 (addImagesToView) 执行它所说的,以及一些 touchesBegan、moved 和 ended 因为棘手。

在 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] 的图像在顶部的卷轴。那是我想要的层次结构。

非常感谢。

最佳答案

您不必为按钮按下编写自己的触摸处理例程。不使用 UIImageView,而是创建 UIButton 并连接到它们的 UIControlEventTouchUpInside 事件。

关于ios - 按钮卡在突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12222191/

相关文章:

ios - 通过本地网络从 iOS 设备浏览桌面文件

iphone - 在 Iphone 中使用页面控件滑动图像(第 2 部分)

ios - 如何在 32 位和 64 位处理器 iOS 之间发送 int

iphone - 从 UIColor 获取 RGBA 颜色分量的更好方法?

iphone - 代码签名错误 : code signing is required for product type 'Application' in SDK 'iOS 4.2'

objective-c - UIScrollView 分页有 2 个 View

iphone - 如何使用 TouchXML 或其他库解析 iPhone 上的 HTML?

ios - 添加一个 SKLabelNode 到 Scene Kit View

ios - Storyboard中的横向和纵向方向

ios - 我不断收到 Thread 1 : Signal SIGABRT; Is there a way to view all IBOutlets in a project?