0

次のようにして、カテゴリを使用して視差効果を行っています。

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

詳細はUIScrollView+Parallax.hの下にあります

    #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

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

そして、私は次のようにしてテーブルに視差を追加しています

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

[self.tableView addParallaxViewWith:pView];

ただし、[self addObserver:forKeyPath:options:context:nil]手がかりがまったくないままアプリをクラッシュさせ続けます。この行をコメントアウトすると、アプリはクラッシュしませんが、視差効果は機能しません。

この問題に対するアイデア。助けてください。ありがとう

4

2 に答える 2

0

コードの問題

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

        return self;
    }

上記のコードで self = [self init]; および [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];

このコードは本当に悪い考えです。このタスクについては、これを参照できます。楽しくきれいなコーディング...

于 2014-03-25T05:16:47.727 に答える