我刚刚开始研究一个项目,我已经遇到了一些问题和错误。我想知道你们是否可以帮助我找出错误的位置。
快速概述:我有两个文件:(ViewController.m和Scroller.m)。在ViewController中,我在ViewDidLoad下有一些代码(我将在一秒钟内显示),我还有一个功能(addImagesToView),它可以完成它所说的内容,还有一些touchesBegan,移动和结束是为了难以处理。
在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 - &gt; backGroundImage - &gt; scroller - &gt; addImagesToView(函数),以便从[I] addImagesToView [/ I]出来的图像位于滚动的顶部。这是我想要的等级制度。
非常感谢。
答案 0 :(得分:2)
您无需为按钮按下编写自己的触摸处理程序。不要使用UIImageView
,而只需创建UIButton
s并挂钩他们的UIControlEventTouchUpInside
事件。
答案 1 :(得分:0)
我认为你最好告诉我们你要完成什么,因为你可能在这里咆哮错误的树。正如吉姆所说,有更好的方法来选择图像,即使你想自己处理触摸,UIGestureRecognizer可能是更好的选择。如果您允许用户选择图像,我建议您在github上查看iCarousel。它是一个开源的旋转木马,非常适合图像选择。此外,如果您可以定位iOS 6,则可以使用新的集合视图。