2012-08-31 40 views
1

我剛剛開始在一個項目上工作,我已經有一些問題和錯誤。我想知道你們是否可以幫我弄清楚錯誤在哪裏。按鈕卡住突出顯示

快速瀏覽:我有兩個文件:(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]出來的圖像開啓滾動的頂部。這是我期望的層次結構。

非常感謝。

回答

2

您不必爲按鈕按下編寫自己的觸摸處理例程。而不是使用UIImageView s,只需創建UIButton s,然後掛接到他們的UIControlEventTouchUpInside事件中。

+0

所以,你覺得用UIButton obj會更好嗎?而不是UIImageViews?我試圖製作的按鈕是自定義的PNG。 – Nactus

+0

是的,使用按鈕,這正是他們設計的。 – Jim

+0

謝謝,我會給它一個鏡頭。 – Nactus

0

我想你最好告訴我們你想要完成什麼,因爲你可能在這裏吠叫錯誤的樹。正如吉姆所說,有更好的方法來選擇圖像,即使你想自己處理觸摸,UIGestureRecognizers可能是一個更好的選擇。如果您允許用戶選擇圖片,我建議您在github上查看iCarousel。這是一個開源的旋轉木馬,非常適合圖像選擇。另外,如果您可以定位iOS 6,那麼您的巷子裏可能會有一個新的收藏視圖。

+0

我的基本想法是在addImagesToView函數中創建一個帶有bg,滾動和一些圖像的視圖。圖像是我在Photoshop中製作的兩個版本的按鈕:普通和突出顯示。你能告訴我更多關於iOS 6的集合視圖嗎?謝謝。 – Nactus