2012-08-31 2 views
1

방금 ​​프로젝트 작업을 시작했으며 이미 몇 가지 문제점과 오류가 있습니다. 너희들이 내가 벌레가 어디 있는지 알아낼 수 있는지 궁금해서.버튼이 계속 강조 표시됨

빠른 개요 : 두 개의 파일 (ViewController.m 및 Scroller.m)이 있습니다. ViewController에서 ViewDidLoad (몇 초 후에 보여줄 것입니다) 아래에 몇 가지 코드가 있는데, 나는 또한 그것이 말한 것을 수행하는 함수 (addImagesToView)를 가지고 있으며 touchesBegan, 이동 및 다루기 힘든 것으로 끝났다.

Scroller에서 "- (void) touch"함수 구현 중 일부를 다시 작성하기로 결정했습니다.

문제점 :은 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 

이 내가 또한의 ViewController의 모든 접촉을 구현하려고 한 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 

,하지만 난 돈 스크롤에서 읽는 방법을 알지 못합니다 (스크롤과 스크롤러의 차이점에 유의하십시오).

[I] addImagesToView [/ I]에서 나온 이미지가 표시되도록 view -> backGroundImage -> scroller -> addImagesToView (함수)와 같이 주문하려고했습니다. 스크롤 상단 그것은 내 원하는 계층 구조입니다.

대단히 감사합니다.

답변

2

버튼 누르기를위한 터치 처리 루틴을 직접 작성할 필요는 없습니다. UIImageView 초를 사용하는 대신 UIButton 초를 작성하고 UIControlEventTouchUpInside 이벤트에 연결하면됩니다.

+0

그래서 UIButton obj를 사용하면 더 잘 작동한다고 생각합니까? 대신 UIImageViews? 내가 만들고자하는 버튼은 사용자 화 된 PNG입니다. – Nactus

+0

예, 버튼을 사용하십시오. 이것은 정확히 설계된 것입니다. – Jim

+0

감사합니다. – Nactus

0

아마도 여기 잘못된 나무를 짖고있는 것처럼 성취하고자하는 것을 알려주는 것이 더 나을 것이라고 생각합니다. Jim이 말했듯이, 이미지를 선택하는 더 좋은 방법이 있습니다. 직접 터치하는 경우에도 UIGestureRecognizers가 더 나은 옵션 일 수 있습니다. 사용자가 이미지를 선택할 수 있도록 허용하는 경우 github의 iCarousel을 살펴 보시기 바랍니다. 그것은 이미지 선택에 아주 좋은 오픈 소스 회전 목마입니다. 또한 iOS 6을 타겟팅 할 수 있다면 골목 위로 올 수있는 새로운 컬렉션보기가 있습니다.

+0

내 기본 생각은 bg, 스크롤 및 addImagesToView 함수에서 나오는 해당 스크롤의 일부 이미지가있는 뷰를 갖는 것입니다. 이미지는 두 가지 버전으로 포토샵에서 만든 버튼입니다 : 일반 및 강조. iOS 6의 컬렉션보기에 대해 좀 더 자세히 설명해 주시겠습니까? 감사. – Nactus

관련 문제