我剛剛開始在一個項目上工作,我已經有一些問題和錯誤。我想知道你們是否可以幫我弄清楚錯誤在哪裏。按鈕卡住突出顯示
快速瀏覽:我有兩個文件:(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
我也試圖實現在視圖控制器的所有接觸,但我不不知道如何從卷軸上讀取它(請注意SCROLL和SCROLLER之間的差異)。你可以告訴我已經試過這樣命令:view - > backGroundImage - > scroller - > addImagesToView(function),這樣從[I] addImagesToView [/ I]出來的圖像開啓滾動的頂部。這是我期望的層次結構。
非常感謝。
所以,你覺得用UIButton obj會更好嗎?而不是UIImageViews?我試圖製作的按鈕是自定義的PNG。 – Nactus
是的,使用按鈕,這正是他們設計的。 – Jim
謝謝,我會給它一個鏡頭。 – Nactus