簡體   English   中英

-[UIView(MyOwnCategory)drawRect:]從未在3.1.3上調用嗎?

[英]Is - [UIView (MyOwnCategory) drawRect:] never called on 3.1.3?

我定義了自己的drawRect方法,並分別在4.2.1(iOS)5.0(iOS)和4.3.2(Simulator)上調用。 但它從未呼吁3.1.3(iPhone 2g)。

這可能是什么原因?

PS:自從我開始寫問題以來,我認為我的3.1.3設備已越獄。 也許這是這種奇怪行為的根本原因。

更新:要重現問題,請使用下一個代碼:

@implementation UIView (MyOwnCategory)
- (void)drawRect:(CGRect)rect
{
    const char * function = __FUNCTION__;
    [NSException raise: @"hi!" format: @"%s", function];
}
@end

即使我顯式調用[super drawRect: rect]也從未在3.1.3上發生過異常

我想寫幾周的方法混戰,@ Kevin Ballard的評論終於使我做到了(感謝您的靈感,Kevin)。

因此,這是使用方法轉換解決您的問題的解決方案,該方法也適用於iOS 3.x:

UIView + Border.h:

#import <Foundation/Foundation.h>
@interface UIView(Border)
@end

UIView + Border.m:

#import "UIView+Border.h"
#import <QuartzCore/QuartzCore.h>
#import <objc/runtime.h>

@implementation UIView(Border)

- (id)swizzled_initWithFrame:(CGRect)frame
{
    // This is the confusing part (article explains this line).
    id result = [self swizzled_initWithFrame:frame];

    // Safe guard: do we have an UIView (or something that has a layer)?
    if ([result respondsToSelector:@selector(layer)]) {
        // Get layer for this view.
        CALayer *layer = [result layer];
        // Set border on layer.
        layer.borderWidth = 2;
        layer.borderColor = [[UIColor redColor] CGColor];
    }

    // Return the modified view.
    return result;
}

- (id)swizzled_initWithCoder:(NSCoder *)aDecoder
{
    // This is the confusing part (article explains this line).
    id result = [self swizzled_initWithCoder:aDecoder];

    // Safe guard: do we have an UIView (or something that has a layer)?
    if ([result respondsToSelector:@selector(layer)]) {
        // Get layer for this view.
        CALayer *layer = [result layer];
        // Set border on layer.
        layer.borderWidth = 2;
        layer.borderColor = [[UIColor blueColor] CGColor];
    }

    // Return the modified view.
    return result;
}

+ (void)load
{
    // The "+ load" method is called once, very early in the application life-cycle.
    // It's called even before the "main" function is called. Beware: there's no
    // autorelease pool at this point, so avoid Objective-C calls.
    Method original, swizzle;

    // Get the "- (id)initWithFrame:" method.
    original = class_getInstanceMethod(self, @selector(initWithFrame:));
    // Get the "- (id)swizzled_initWithFrame:" method.
    swizzle = class_getInstanceMethod(self, @selector(swizzled_initWithFrame:));
    // Swap their implementations.
    method_exchangeImplementations(original, swizzle);

    // Get the "- (id)initWithCoder:" method.
    original = class_getInstanceMethod(self, @selector(initWithCoder:));
    // Get the "- (id)swizzled_initWithCoder:" method.
    swizzle = class_getInstanceMethod(self, @selector(swizzled_initWithCoder:));
    // Swap their implementations.
    method_exchangeImplementations(original, swizzle);
}

@end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM