Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 94 lines (68 sloc) 2.538 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
//
// UIControl+HLSExclusiveTouch.m
// nut-dev
//
// Created by Samuel Défago on 07.06.11.
// Copyright 2011 Hortis. All rights reserved.
//

#import "UIControl+HLSExclusiveTouch.h"

#import <objc/runtime.h>

static BOOL m_injected = NO;

// Original implementation of the methods we swizzle
static IMP s_initWithFrame$Imp;
static IMP s_initWithCoder$Imp;

// Static methods
static void swizzleSelector(Class class, SEL origSel, SEL newSel);

#pragma mark -
#pragma mark UIControl (HLSExclusiveTouchPrivate) interface

@interface UIButton (HLSExclusiveTouchPrivate)

- (id)swizzledInitWithFrame:(CGRect)frame;
- (id)swizzledInitWithCoder:(NSCoder *)aDecoder;

@end

#pragma mark -
#pragma mark UIControl (HLSExclusiveTouch) implementation

@implementation UIControl (HLSExclusiveTouch)

#pragma mark Class methods

+ (void)injectExclusiveTouch
{
    if (m_injected) {
        return;
    }
    
    // Get the original implementations we want to swizzle
    s_initWithFrame$Imp = method_getImplementation(class_getInstanceMethod([self class],
                                                                           @selector(initWithFrame:)));
    s_initWithCoder$Imp = method_getImplementation(class_getInstanceMethod([self class],
                                                                           @selector(initWithCoder:)));
    
    // Swizzle with custom wrappers
    swizzleSelector([self class], @selector(initWithFrame:), @selector(swizzledInitWithFrame:));
    swizzleSelector([self class], @selector(initWithCoder:), @selector(swizzledInitWithCoder:));
    
    m_injected = YES;
}

@end

#pragma mark -
#pragma mark UIControl (HLSExclusiveTouchPrivate) implementation

@implementation UIControl (HLSExclusiveTouchPrivate)

#pragma mark Methods injected by swizzling

- (id)swizzledInitWithFrame:(CGRect)frame
{
    // Call the original implementation
    if ((self = (*s_initWithFrame$Imp)(self, @selector(initWithFrame:), frame))) {
        self.exclusiveTouch = YES;
    }
    return self;
}

- (id)swizzledInitWithCoder:(NSCoder *)aDecoder
{
    // Call the original implementation
    if ((self = (*s_initWithCoder$Imp)(self, @selector(initWithCoder:), aDecoder))) {
        self.exclusiveTouch = YES;
    }
    return self;
}

@end

#pragma mark Swizzler

static void swizzleSelector(Class class, SEL origSel, SEL newSel)
{
    Method newMethod = class_getInstanceMethod(class, newSel);
    class_replaceMethod(class, origSel, method_getImplementation(newMethod), method_getTypeEncoding(newMethod));
}
Something went wrong with that request. Please try again.