2014-03-25 2 views
0

내가 수행하여 범주를 사용하여 시차 효과를하고있는 중이 야 :addObserver : forKeyPath 앱 충돌되고, KVO, 목표 C

add and UIView into the uitableView (via category 
    add addObserver:forKeyPath so that whenever tableview is moving, i will reframe the view above 

세부 있는 UIScrollView + 아래

#import <UIKit/UIKit.h> 

    @class ParallaxView; 
    @interface UIScrollView (Parallax) 

    @property (strong, nonatomic) ParallaxView *parallaxView; 
    - (void) addParallaxViewWith:(UIView*)parallaxView; 
    - (void) removeKVO; 

    @end 


    @interface ParallaxView : UIView 

    @end 

Parallax.h 있습니다 UIScrollView + Parallax.m

static char parallaxKey; 
    @implementation UIScrollView (Parallax) 
    @dynamic parallaxView; 


    #pragma mark - Add parallax view to scrollView 
    - (void) addParallaxViewWith:(ParallaxView*)pView { 
     if (!self.parallaxView) { 

      [self addSubview:pView]; 
      [self setParallaxView:pView]; 
     } 

    } 

    #pragma mark - Set parallaxView + register parallaxView as an observer 
    - (void) setParallaxView:(ParallaxView *)parallaxView { 
     objc_setAssociatedObject(self, &parallaxKey, parallaxView, OBJC_ASSOCIATION_ASSIGN); 
    /* THESE LINE ARE CRASHING THE APP */ 
    // [self addObserver:self.parallaxView 
    //   forKeyPath:@"contentOffset" 
    //    options:NSKeyValueObservingOptionNew 
    //    context:nil]; 
    } 

    #pragma mark - Get parallaxView 
    - (ParallaxView*) parallaxView { 
     return (objc_getAssociatedObject(self, &parallaxKey)); 
    } 
    #pragma mark - Remove 
    - (void)removeKVO { 
      [self removeObserver:self.parallaxView forKeyPath:@"contentOffset"]; 
    } 

    @end 

    @implementation ParallaxView 

    -(id)init 
    { 
     //load xib from main bundle and assign it to self 
     self = [[[NSBundle mainBundle]loadNibNamed:@"Parallex" 
              owner:self 
              options:nil] objectAtIndex:0]; 

     return self; 
    } 

    -(id)initWithFrame:(CGRect)frame 
    { 
     self = [self init]; 
     [self setFrame:frame]; 

     return self; 
    } 

    ................ 

    @end 

그리고 doi가 테이블에 시차를 추가하고 있습니다. NG

ParallaxView *pView = [[ParallaxView alloc]initWithFrame:CGRectMake(0, 0, 320, 160)]; 

[self.tableView addParallaxViewWith:pView]; 

그러나, [self addObserver:forKeyPath:options:context:nil]는 전혀 단서없이 응용 프로그램을 충돌 유지합니다. 이 줄을 주석으로 처리하면 응용 프로그램이 충돌하지 않지만 parallex 효과가 작동하지 않습니다.

이 문제에 대한 아이디어. 도와주세요. 감사합니다

답변

0
@implementation ParallaxView 

//Add Observe Method 
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ 
    if([keyPath isEqualToString:@"contentOffset"]){ 
     NSLog(@"contentOffset:%@", [change objectForKey:NSKeyValueChangeNewKey]); 
    } 
} 

@end 

봅니다 강한 참조해야

//Change to OBJC_ASSOCIATION_RETAIN_NONATOMIC 
objc_setAssociatedObject(self, &parallaxKey, parallaxView, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 

parallaxView

objc_setAssociatedObject(self, &parallaxKey, parallaxView, OBJC_ASSOCIATION_ASSIGN); 

을 대체한다. 상기 코드 자체 = 자기 INIT] 코드

-(id)initWithFrame:(CGRect)frame 
    { 
     self = [self init]; 
     [self setFrame:frame]; 

     return self; 
    } 

+0

simalone : 도움 주셔서 감사합니다. 불행히도, 나는 그것을 시도하고 여전히 충돌을 준 ... – tranvutuan

+0

나는 새로운 테스트 프로젝트에서 코드를 시도하고 어딘가 편집 후 작동 내 편집 답변을 참조하십시오. 그리고 내 init 메소드는 펜촉에서 슈퍼를 호출하지 않습니다. 여전히 크래시가 있으면 펜촉을 확인하십시오. – simalone

0

문제; 및 [self setFrame : frame]; 충돌 을 줄 것이다 재귀에 갈 것이다, 첫째는이

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 

같이해야한다, 나는 그것이 문제를 해결할 생각이 문제를 해결하고이 코드가 정말

self = [[[NSBundle mainBundle]loadNibNamed:@"Parallex" 
              owner:self 
              options:nil] objectAtIndex:0]; 

를 사용하여 펜촉에서보기를로드 나쁜 생각이야. THIS을 참조 할 수 있습니다. 행복하고 깨끗한 코딩 ...