Skip to content
Browse files

Added block based action handlers

  • Loading branch information...
1 parent 02bceae commit b2453217a4c428b9b4b83111842bc4bbcf7fa431 @odrobnik odrobnik committed Jun 5, 2012
View
27 Core/Source/UIView+DTActionHandlers.h
@@ -0,0 +1,27 @@
+//
+// UIView+DTActionHandlers.h
+// DTFoundation
+//
+// Created by Oliver Drobnik on 03.06.12.
+// Copyright (c) 2012 Cocoanetics. All rights reserved.
+//
+
+/**
+ Methods to add simple block-based actions to UIViews.
+ */
+
+@interface UIView (DTActionHandlers)
+
+/**
+ Attaches the given block for a single tap action to the receiver.
+ @param block The block to execute.
+ */
+- (void)setTapActionWithBlock:(void (^)(void))block;
+
+/**
+ Attaches the given block for a long press action to the receiver.
+ @param block The block to execute.
+ */
+- (void)setLongPressActionWithBlock:(void (^)(void))block;
+
+@end
View
77 Core/Source/UIView+DTActionHandlers.m
@@ -0,0 +1,77 @@
+//
+// UIView+DTActionHandlers.m
+// DTFoundation
+//
+// Created by Oliver Drobnik on 03.06.12.
+// Copyright (c) 2012 Cocoanetics. All rights reserved.
+//
+
+#import "UIView+DTActionHandlers.h"
+
+#import <objc/runtime.h>
+
+// force this category to be loaded by linker
+MAKE_CATEGORIES_LOADABLE(UIView_DTActionHandlers);
+
+char * const kDTActionHandlerTapBlockKey = "DTActionHandlerTapBlockKey";
+char * const kDTActionHandlerTapGestureKey = "DTActionHandlerTapGestureKey";
+char * const kDTActionHandlerLongPressBlockKey = "DTActionHandlerLongPressBlockKey";
+char * const kDTActionHandlerLongPressGestureKey = "DTActionHandlerLongPressGestureKey";
+
+@implementation UIView (DTActionHandlers)
+
+- (void)setTapActionWithBlock:(void (^)(void))block
+{
+ UITapGestureRecognizer *gesture = objc_getAssociatedObject(self, kDTActionHandlerTapGestureKey);
+
+ if (!gesture)
+ {
+ gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(__handleActionForTapGesture:)];
+ [self addGestureRecognizer:gesture];
+ objc_setAssociatedObject(self, kDTActionHandlerTapGestureKey, gesture, OBJC_ASSOCIATION_RETAIN);
+ }
+
+ objc_setAssociatedObject(self, kDTActionHandlerTapBlockKey, block, OBJC_ASSOCIATION_COPY);
+}
+
+- (void)__handleActionForTapGesture:(UITapGestureRecognizer *)gesture
+{
+ if (gesture.state == UIGestureRecognizerStateRecognized)
+ {
+ void(^action)(void) = objc_getAssociatedObject(self, kDTActionHandlerTapBlockKey);
+
+ if (action)
+ {
+ action();
+ }
+ }
+}
+
+- (void)setLongPressActionWithBlock:(void (^)(void))block
+{
+ UILongPressGestureRecognizer *gesture = objc_getAssociatedObject(self, kDTActionHandlerLongPressGestureKey);
+
+ if (!gesture)
+ {
+ gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(__handleActionForLongPressGesture:)];
+ [self addGestureRecognizer:gesture];
+ objc_setAssociatedObject(self, kDTActionHandlerLongPressGestureKey, gesture, OBJC_ASSOCIATION_RETAIN);
+ }
+
+ objc_setAssociatedObject(self, kDTActionHandlerLongPressBlockKey, block, OBJC_ASSOCIATION_COPY);
+}
+
+- (void)__handleActionForLongPressGesture:(UITapGestureRecognizer *)gesture
+{
+ if (gesture.state == UIGestureRecognizerStateBegan)
+ {
+ void(^action)(void) = objc_getAssociatedObject(self, kDTActionHandlerLongPressBlockKey);
+
+ if (action)
+ {
+ action();
+ }
+ }
+}
+
+@end
View
24 DTFoundation.xcodeproj/project.pbxproj
@@ -91,6 +91,10 @@
A77DD42514E825FC00F34B03 /* zip.c in Sources */ = {isa = PBXBuildFile; fileRef = A77DD41414E825FC00F34B03 /* zip.c */; };
A77DD42614E825FC00F34B03 /* zip.h in Headers */ = {isa = PBXBuildFile; fileRef = A77DD41514E825FC00F34B03 /* zip.h */; };
A77DD42714E825FC00F34B03 /* zip.h in Headers */ = {isa = PBXBuildFile; fileRef = A77DD41514E825FC00F34B03 /* zip.h */; };
+ A78DF5C5157BBC5B00E360B5 /* UIView+DTActionHandlers.h in Headers */ = {isa = PBXBuildFile; fileRef = A78DF5C3157BBC5B00E360B5 /* UIView+DTActionHandlers.h */; };
+ A78DF5C6157BBC5B00E360B5 /* UIView+DTActionHandlers.h in Headers */ = {isa = PBXBuildFile; fileRef = A78DF5C3157BBC5B00E360B5 /* UIView+DTActionHandlers.h */; };
+ A78DF5C7157BBC5B00E360B5 /* UIView+DTActionHandlers.m in Sources */ = {isa = PBXBuildFile; fileRef = A78DF5C4157BBC5B00E360B5 /* UIView+DTActionHandlers.m */; };
+ A78DF5C8157BBC5B00E360B5 /* UIView+DTActionHandlers.m in Sources */ = {isa = PBXBuildFile; fileRef = A78DF5C4157BBC5B00E360B5 /* UIView+DTActionHandlers.m */; };
A79231CE157A0B9400C3ACBB /* NSURL+DTUnshorten.h in Headers */ = {isa = PBXBuildFile; fileRef = A79231CC157A0B9400C3ACBB /* NSURL+DTUnshorten.h */; };
A79231CF157A0B9400C3ACBB /* NSURL+DTUnshorten.h in Headers */ = {isa = PBXBuildFile; fileRef = A79231CC157A0B9400C3ACBB /* NSURL+DTUnshorten.h */; };
A79231D0157A0B9400C3ACBB /* NSURL+DTUnshorten.m in Sources */ = {isa = PBXBuildFile; fileRef = A79231CD157A0B9400C3ACBB /* NSURL+DTUnshorten.m */; };
@@ -103,14 +107,14 @@
A7A7CC7A14866CAF00EC2EE4 /* NSString+DTFormatNumbers.m in Sources */ = {isa = PBXBuildFile; fileRef = A70B4CCB1486621B00873A4A /* NSString+DTFormatNumbers.m */; };
A7A7CC7B14866CAF00EC2EE4 /* NSURL+DTAppLinks.m in Sources */ = {isa = PBXBuildFile; fileRef = A70B4CCD1486621B00873A4A /* NSURL+DTAppLinks.m */; };
A7A7CC7C14866CAF00EC2EE4 /* NSURL+DTPrefLinks.m in Sources */ = {isa = PBXBuildFile; fileRef = A70B4CCF1486621B00873A4A /* NSURL+DTPrefLinks.m */; };
- A7B57EDA157004630091D4EF /* UIWebView+DTFoundation.h in Headers */ = {isa = PBXBuildFile; fileRef = A7B57ED8157004630091D4EF /* UIWebView+DTFoundation.h */; };
- A7B57EDB157004630091D4EF /* UIWebView+DTFoundation.h in Headers */ = {isa = PBXBuildFile; fileRef = A7B57ED8157004630091D4EF /* UIWebView+DTFoundation.h */; };
- A7B57EDC157004630091D4EF /* UIWebView+DTFoundation.m in Sources */ = {isa = PBXBuildFile; fileRef = A7B57ED9157004630091D4EF /* UIWebView+DTFoundation.m */; };
- A7B57EDD157004630091D4EF /* UIWebView+DTFoundation.m in Sources */ = {isa = PBXBuildFile; fileRef = A7B57ED9157004630091D4EF /* UIWebView+DTFoundation.m */; };
A7AB8BFA156AA00700CBAB7E /* UIApplication+DTNetworkActivity.h in Headers */ = {isa = PBXBuildFile; fileRef = A7AB8BF8156AA00700CBAB7E /* UIApplication+DTNetworkActivity.h */; };
A7AB8BFB156AA00700CBAB7E /* UIApplication+DTNetworkActivity.h in Headers */ = {isa = PBXBuildFile; fileRef = A7AB8BF8156AA00700CBAB7E /* UIApplication+DTNetworkActivity.h */; };
A7AB8BFC156AA00700CBAB7E /* UIApplication+DTNetworkActivity.m in Sources */ = {isa = PBXBuildFile; fileRef = A7AB8BF9156AA00700CBAB7E /* UIApplication+DTNetworkActivity.m */; };
A7AB8BFD156AA00700CBAB7E /* UIApplication+DTNetworkActivity.m in Sources */ = {isa = PBXBuildFile; fileRef = A7AB8BF9156AA00700CBAB7E /* UIApplication+DTNetworkActivity.m */; };
+ A7B57EDA157004630091D4EF /* UIWebView+DTFoundation.h in Headers */ = {isa = PBXBuildFile; fileRef = A7B57ED8157004630091D4EF /* UIWebView+DTFoundation.h */; };
+ A7B57EDB157004630091D4EF /* UIWebView+DTFoundation.h in Headers */ = {isa = PBXBuildFile; fileRef = A7B57ED8157004630091D4EF /* UIWebView+DTFoundation.h */; };
+ A7B57EDC157004630091D4EF /* UIWebView+DTFoundation.m in Sources */ = {isa = PBXBuildFile; fileRef = A7B57ED9157004630091D4EF /* UIWebView+DTFoundation.m */; };
+ A7B57EDD157004630091D4EF /* UIWebView+DTFoundation.m in Sources */ = {isa = PBXBuildFile; fileRef = A7B57ED9157004630091D4EF /* UIWebView+DTFoundation.m */; };
A7D0AA25153C1B160020F18B /* NSDictionary+DTError.h in Headers */ = {isa = PBXBuildFile; fileRef = A7D0AA23153C1B160020F18B /* NSDictionary+DTError.h */; settings = {ATTRIBUTES = (Public, ); }; };
A7D0AA26153C1B160020F18B /* NSDictionary+DTError.h in Headers */ = {isa = PBXBuildFile; fileRef = A7D0AA23153C1B160020F18B /* NSDictionary+DTError.h */; settings = {ATTRIBUTES = (Public, ); }; };
A7D0AA27153C1B160020F18B /* NSDictionary+DTError.m in Sources */ = {isa = PBXBuildFile; fileRef = A7D0AA24153C1B160020F18B /* NSDictionary+DTError.m */; };
@@ -198,14 +202,16 @@
A77DD41314E825FC00F34B03 /* unzip.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = unzip.h; sourceTree = "<group>"; };
A77DD41414E825FC00F34B03 /* zip.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = zip.c; sourceTree = "<group>"; };
A77DD41514E825FC00F34B03 /* zip.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = zip.h; sourceTree = "<group>"; };
+ A78DF5C3157BBC5B00E360B5 /* UIView+DTActionHandlers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIView+DTActionHandlers.h"; sourceTree = "<group>"; };
+ A78DF5C4157BBC5B00E360B5 /* UIView+DTActionHandlers.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIView+DTActionHandlers.m"; sourceTree = "<group>"; };
A79231CC157A0B9400C3ACBB /* NSURL+DTUnshorten.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSURL+DTUnshorten.h"; sourceTree = "<group>"; };
A79231CD157A0B9400C3ACBB /* NSURL+DTUnshorten.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSURL+DTUnshorten.m"; sourceTree = "<group>"; };
A7949A3814C963F500A8CCDE /* DTHTMLParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DTHTMLParser.h; sourceTree = "<group>"; };
A7949A3914C963F500A8CCDE /* DTHTMLParser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DTHTMLParser.m; sourceTree = "<group>"; };
- A7B57ED8157004630091D4EF /* UIWebView+DTFoundation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIWebView+DTFoundation.h"; sourceTree = "<group>"; };
- A7B57ED9157004630091D4EF /* UIWebView+DTFoundation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIWebView+DTFoundation.m"; sourceTree = "<group>"; };
A7AB8BF8156AA00700CBAB7E /* UIApplication+DTNetworkActivity.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIApplication+DTNetworkActivity.h"; sourceTree = "<group>"; };
A7AB8BF9156AA00700CBAB7E /* UIApplication+DTNetworkActivity.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIApplication+DTNetworkActivity.m"; sourceTree = "<group>"; };
+ A7B57ED8157004630091D4EF /* UIWebView+DTFoundation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIWebView+DTFoundation.h"; sourceTree = "<group>"; };
+ A7B57ED9157004630091D4EF /* UIWebView+DTFoundation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIWebView+DTFoundation.m"; sourceTree = "<group>"; };
A7BAD10E1483F934000E2B6A /* DTFoundation.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = DTFoundation.framework; sourceTree = BUILT_PRODUCTS_DIR; };
A7D0AA23153C1B160020F18B /* NSDictionary+DTError.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSDictionary+DTError.h"; sourceTree = "<group>"; };
A7D0AA24153C1B160020F18B /* NSDictionary+DTError.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSDictionary+DTError.m"; sourceTree = "<group>"; };
@@ -370,6 +376,8 @@
A72C110414A4946800F4EF69 /* UIView+DTFoundation.m */,
A7B57ED8157004630091D4EF /* UIWebView+DTFoundation.h */,
A7B57ED9157004630091D4EF /* UIWebView+DTFoundation.m */,
+ A78DF5C3157BBC5B00E360B5 /* UIView+DTActionHandlers.h */,
+ A78DF5C4157BBC5B00E360B5 /* UIView+DTActionHandlers.m */,
);
name = Categories;
sourceTree = "<group>";
@@ -469,6 +477,7 @@
A7B57EDB157004630091D4EF /* UIWebView+DTFoundation.h in Headers */,
A7AB8BFB156AA00700CBAB7E /* UIApplication+DTNetworkActivity.h in Headers */,
A79231CF157A0B9400C3ACBB /* NSURL+DTUnshorten.h in Headers */,
+ A78DF5C6157BBC5B00E360B5 /* UIView+DTActionHandlers.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -511,6 +520,7 @@
A7B57EDA157004630091D4EF /* UIWebView+DTFoundation.h in Headers */,
A7AB8BFA156AA00700CBAB7E /* UIApplication+DTNetworkActivity.h in Headers */,
A79231CE157A0B9400C3ACBB /* NSURL+DTUnshorten.h in Headers */,
+ A78DF5C5157BBC5B00E360B5 /* UIView+DTActionHandlers.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -672,6 +682,7 @@
A7B57EDD157004630091D4EF /* UIWebView+DTFoundation.m in Sources */,
A7AB8BFD156AA00700CBAB7E /* UIApplication+DTNetworkActivity.m in Sources */,
A79231D1157A0B9400C3ACBB /* NSURL+DTUnshorten.m in Sources */,
+ A78DF5C8157BBC5B00E360B5 /* UIView+DTActionHandlers.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -710,6 +721,7 @@
A7B57EDC157004630091D4EF /* UIWebView+DTFoundation.m in Sources */,
A7AB8BFC156AA00700CBAB7E /* UIApplication+DTNetworkActivity.m in Sources */,
A79231D0157A0B9400C3ACBB /* NSURL+DTUnshorten.m in Sources */,
+ A78DF5C7157BBC5B00E360B5 /* UIView+DTActionHandlers.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

0 comments on commit b245321

Please sign in to comment.
Something went wrong with that request. Please try again.