From 603587e798e929bdd5cd0ed41471bf3d4809815c Mon Sep 17 00:00:00 2001 From: Constantine Mureev Date: Fri, 23 Sep 2011 16:59:49 +0400 Subject: [PATCH] add blocks api intead delegates --- ATMHud.h | 8 ++-- ATMHud.m | 16 ++++---- ATMHud.xcodeproj/project.pbxproj | 17 +++++--- ATMHudBlock.h | 47 +++++++++++++++++++++++ ATMHudBlock.m | 66 ++++++++++++++++++++++++++++++++ ATMHudDelegate.h | 25 ------------ ATMHudView.m | 40 +++++++++---------- Classes/DemoViewController.h | 4 +- Classes/DemoViewController.m | 10 ++--- 9 files changed, 162 insertions(+), 71 deletions(-) create mode 100644 ATMHudBlock.h create mode 100644 ATMHudBlock.m delete mode 100644 ATMHudDelegate.h diff --git a/ATMHud.h b/ATMHud.h index ebc5476..e1849d3 100644 --- a/ATMHud.h +++ b/ATMHud.h @@ -10,8 +10,8 @@ */ #import +#import "ATMHudBlock.h" @class ATMHudView, ATMSoundFX, ATMHudQueueItem; -@protocol ATMHudDelegate; typedef enum { ATMHudAccessoryPositionTop = 0, @@ -41,7 +41,7 @@ typedef enum { NSString *updateSound; NSString *hideSound; - id delegate; + ATMHudBlock* block; ATMHudAccessoryPosition accessoryPosition; @private @@ -71,7 +71,7 @@ typedef enum { @property (nonatomic, retain) NSString *updateSound; @property (nonatomic, retain) NSString *hideSound; -@property (nonatomic, assign) id delegate; +@property (nonatomic, retain) ATMHudBlock* block; @property (nonatomic, assign) ATMHudAccessoryPosition accessoryPosition; @property (nonatomic, retain) ATMHudView *__view; @@ -81,7 +81,7 @@ typedef enum { + (NSString *)buildInfo; -- (id)initWithDelegate:(id)hudDelegate; +- (id)initWithBlock:(ATMHudBlock*)hudBlock; - (void)setCaption:(NSString *)caption; - (void)setImage:(UIImage *)image; diff --git a/ATMHud.m b/ATMHud.m index 4c29853..d0d389f 100644 --- a/ATMHud.m +++ b/ATMHud.m @@ -14,7 +14,7 @@ #import #import "ATMHudView.h" #import "ATMProgressLayer.h" -#import "ATMHudDelegate.h" +#import "ATMHudBlock.h" #import "ATMSoundFX.h" #import "ATMHudQueueItem.h" @@ -24,7 +24,7 @@ - (void)construct; @implementation ATMHud @synthesize margin, padding, alpha, appearScaleFactor, disappearScaleFactor, progressBorderRadius, progressBorderWidth, progressBarRadius, progressBarInset; -@synthesize delegate, accessoryPosition; +@synthesize block, accessoryPosition; @synthesize center; @synthesize shadowEnabled, blockTouches, allowSuperviewInteraction; @synthesize showSound, updateSound, hideSound; @@ -37,9 +37,9 @@ - (id)init { return self; } -- (id)initWithDelegate:(id)hudDelegate { +- (id)initWithBlock:(ATMHudBlock*)hudBlock { if ((self = [super init])) { - delegate = hudDelegate; + self.block = hudBlock; [self construct]; } return self; @@ -74,6 +74,8 @@ - (void)viewDidUnload { } - (void)dealloc { + self.block = nil; + [sound release]; [__view release]; [displayQueue release]; @@ -282,9 +284,9 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { if (aTouch.tapCount == 1) { CGPoint p = [aTouch locationInView:self.view]; if (CGRectContainsPoint(__view.frame, p)) { - if ([(id)self.delegate respondsToSelector:@selector(userDidTapHud:)]) { - [self.delegate userDidTapHud:self]; - } + if (self.block && self.block.tapBlock) { + self.block.tapBlock(self); + } } } } diff --git a/ATMHud.xcodeproj/project.pbxproj b/ATMHud.xcodeproj/project.pbxproj index 82ca58d..b8ee583 100755 --- a/ATMHud.xcodeproj/project.pbxproj +++ b/ATMHud.xcodeproj/project.pbxproj @@ -3,7 +3,7 @@ archiveVersion = 1; classes = { }; - objectVersion = 45; + objectVersion = 46; objects = { /* Begin PBXBuildFile section */ @@ -27,6 +27,7 @@ 1FEF4B9B131CD67500C59266 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1FEF4B9A131CD67500C59266 /* QuartzCore.framework */; }; 288765FD0DF74451002DB57D /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 288765FC0DF74451002DB57D /* CoreGraphics.framework */; }; 28AD733F0D9D9553002E5188 /* MainWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 28AD733E0D9D9553002E5188 /* MainWindow.xib */; }; + AD39D20F142CB09B00C65B9B /* ATMHudBlock.m in Sources */ = {isa = PBXBuildFile; fileRef = AD39D20E142CB09B00C65B9B /* ATMHudBlock.m */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -44,7 +45,6 @@ 1FEF4B72131CD64B00C59266 /* DemoViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DemoViewController.m; sourceTree = ""; }; 1FEF4B74131CD65A00C59266 /* ATMHud.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ATMHud.h; sourceTree = ""; }; 1FEF4B75131CD65A00C59266 /* ATMHud.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ATMHud.m; sourceTree = ""; }; - 1FEF4B76131CD65A00C59266 /* ATMHudDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ATMHudDelegate.h; sourceTree = ""; }; 1FEF4B77131CD65A00C59266 /* ATMHudQueueItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ATMHudQueueItem.h; sourceTree = ""; }; 1FEF4B78131CD65A00C59266 /* ATMHudQueueItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ATMHudQueueItem.m; sourceTree = ""; }; 1FEF4B79131CD65A00C59266 /* ATMHudView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ATMHudView.h; sourceTree = ""; }; @@ -62,6 +62,8 @@ 29B97316FDCFA39411CA2CEA /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 32CA4F630368D1EE00C91783 /* ATMHud_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ATMHud_Prefix.pch; sourceTree = ""; }; 8D1107310486CEB800E47090 /* ATMHud-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "ATMHud-Info.plist"; plistStructureDefinitionIdentifier = "com.apple.xcode.plist.structure-definition.iphone.info-plist"; sourceTree = ""; }; + AD39D20D142CB09B00C65B9B /* ATMHudBlock.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ATMHudBlock.h; sourceTree = ""; }; + AD39D20E142CB09B00C65B9B /* ATMHudBlock.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ATMHudBlock.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -104,7 +106,6 @@ children = ( 1FEF4B74131CD65A00C59266 /* ATMHud.h */, 1FEF4B75131CD65A00C59266 /* ATMHud.m */, - 1FEF4B76131CD65A00C59266 /* ATMHudDelegate.h */, 1FEF4B77131CD65A00C59266 /* ATMHudQueueItem.h */, 1FEF4B78131CD65A00C59266 /* ATMHudQueueItem.m */, 1FEF4B79131CD65A00C59266 /* ATMHudView.h */, @@ -115,6 +116,8 @@ 1FEF4B7E131CD65A00C59266 /* ATMSoundFX.m */, 1FEF4B7F131CD65A00C59266 /* ATMTextLayer.h */, 1FEF4B80131CD65A00C59266 /* ATMTextLayer.m */, + AD39D20D142CB09B00C65B9B /* ATMHudBlock.h */, + AD39D20E142CB09B00C65B9B /* ATMHudBlock.m */, ); name = ATMHud; sourceTree = ""; @@ -192,8 +195,11 @@ /* Begin PBXProject section */ 29B97313FDCFA39411CA2CEA /* Project object */ = { isa = PBXProject; + attributes = { + LastUpgradeCheck = 0410; + }; buildConfigurationList = C01FCF4E08A954540054247B /* Build configuration list for PBXProject "ATMHud" */; - compatibilityVersion = "Xcode 3.1"; + compatibilityVersion = "Xcode 3.2"; developmentRegion = English; hasScannedForEncodings = 1; knownRegions = ( @@ -241,6 +247,7 @@ 1FEF4B84131CD65A00C59266 /* ATMProgressLayer.m in Sources */, 1FEF4B85131CD65A00C59266 /* ATMSoundFX.m in Sources */, 1FEF4B86131CD65A00C59266 /* ATMTextLayer.m in Sources */, + AD39D20F142CB09B00C65B9B /* ATMHudBlock.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -284,7 +291,6 @@ GCC_WARN_ABOUT_RETURN_TYPE = YES; GCC_WARN_UNUSED_VARIABLE = YES; IPHONEOS_DEPLOYMENT_TARGET = 4.0; - PREBINDING = NO; SDKROOT = iphoneos; }; name = Debug; @@ -300,7 +306,6 @@ GCC_WARN_UNUSED_VARIABLE = YES; IPHONEOS_DEPLOYMENT_TARGET = 4.0; OTHER_CFLAGS = "-DNS_BLOCK_ASSERTIONS=1"; - PREBINDING = NO; SDKROOT = iphoneos; }; name = Release; diff --git a/ATMHudBlock.h b/ATMHudBlock.h new file mode 100644 index 0000000..d93454b --- /dev/null +++ b/ATMHudBlock.h @@ -0,0 +1,47 @@ +// +// ATMHudBlock.h +// ATMHud +// +// Created by Constantine on 9/23/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import + +@class ATMHud; + +@protocol ATMHudCallback ++ (id)blockWithUserDidTapHud:(id)tapBlock willAppear:(id)willAppearBlock didAppear:(id)didAppearBlock willUpdate:(id)willUpdateBlock didUpdate:(id)didUpdateBlock willDisappear:(id)willDisappearBlock didDisappear:(id)didDisappearBlock; +@end + +@interface ATMHudCallback : NSObject { +@private + id _tapBlock; + id _willAppearBlock; + id _didAppearBlock; + id _willUpdateBlock; + id _didUpdateBlock; + id _willDisappearBlock; + id _didDisappearBlock; +} + +@end + +typedef void (^ATMHudDefaultBlock)(ATMHud *hud); + +#pragma mark - ATMHudBlock + +@protocol ATMHudBlock +@optional ++ (id)blockWithUserDidTapHud:(ATMHudDefaultBlock)tapBlock willAppear:(ATMHudDefaultBlock)willAppearBlock didAppear:(ATMHudDefaultBlock)didAppearBlock willUpdate:(ATMHudDefaultBlock)willUpdateBlock didUpdate:(ATMHudDefaultBlock)didUpdateBlock willDisappear:(ATMHudDefaultBlock)willDisappearBlock didDisappear:(ATMHudDefaultBlock)didDisappearBlock; +@end + +@interface ATMHudBlock : ATMHudCallback +@property (readwrite, nonatomic, copy) ATMHudDefaultBlock tapBlock; +@property (readwrite, nonatomic, copy) ATMHudDefaultBlock willAppearBlock; +@property (readwrite, nonatomic, copy) ATMHudDefaultBlock didAppearBlock; +@property (readwrite, nonatomic, copy) ATMHudDefaultBlock willUpdateBlock; +@property (readwrite, nonatomic, copy) ATMHudDefaultBlock didUpdateBlock; +@property (readwrite, nonatomic, copy) ATMHudDefaultBlock willDisappearBlock; +@property (readwrite, nonatomic, copy) ATMHudDefaultBlock didDisappearBlock; +@end \ No newline at end of file diff --git a/ATMHudBlock.m b/ATMHudBlock.m new file mode 100644 index 0000000..f031f0f --- /dev/null +++ b/ATMHudBlock.m @@ -0,0 +1,66 @@ +// +// ATMHudBlock.m +// ATMHud +// +// Created by Constantine on 9/23/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import "ATMHudBlock.h" + +@interface ATMHudCallback () +@property (readwrite, nonatomic, copy) id tapBlock; +@property (readwrite, nonatomic, copy) id willAppearBlock; +@property (readwrite, nonatomic, copy) id didAppearBlock; +@property (readwrite, nonatomic, copy) id willUpdateBlock; +@property (readwrite, nonatomic, copy) id didUpdateBlock; +@property (readwrite, nonatomic, copy) id willDisappearBlock; +@property (readwrite, nonatomic, copy) id didDisappearBlock; +@end + +@implementation ATMHudCallback +@synthesize tapBlock = _tapBlock; +@synthesize willAppearBlock = _willAppearBlock; +@synthesize didAppearBlock = _didAppearBlock; +@synthesize willUpdateBlock = _willUpdateBlock; +@synthesize didUpdateBlock = _didUpdateBlock; +@synthesize willDisappearBlock = _willDisappearBlock; +@synthesize didDisappearBlock = _didDisappearBlock; + ++ (id)blockWithUserDidTapHud:(id)tapBlock willAppear:(id)willAppearBlock didAppear:(id)didAppearBlock willUpdate:(id)willUpdateBlock didUpdate:(id)didUpdateBlock willDisappear:(id)willDisappearBlock didDisappear:(id)didDisappearBlock { + id callback = [[[self alloc] init] autorelease]; + [callback setTapBlock:tapBlock]; + [callback setWillAppearBlock:willAppearBlock]; + [callback setDidAppearBlock:didAppearBlock]; + [callback setWillUpdateBlock:willUpdateBlock]; + [callback setDidUpdateBlock:didUpdateBlock]; + [callback setWillDisappearBlock:willDisappearBlock]; + [callback setDidDisappearBlock:didDisappearBlock]; + + return callback; +} + +- (id)init { + if ([self class] == [ATMHudCallback class]) { + [NSException raise:NSInternalInconsistencyException format:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)]; + } + + return [super init]; +} + +- (void)dealloc { + [_tapBlock release]; + [_willAppearBlock release]; + [_didAppearBlock release]; + [_willUpdateBlock release]; + [_didUpdateBlock release]; + [_willDisappearBlock release]; + [_didDisappearBlock release]; + [super dealloc]; +} + +@end + +@implementation ATMHudBlock +@dynamic tapBlock, willAppearBlock, didAppearBlock, willUpdateBlock, didUpdateBlock, willDisappearBlock, didDisappearBlock; +@end diff --git a/ATMHudDelegate.h b/ATMHudDelegate.h deleted file mode 100644 index 2d86aad..0000000 --- a/ATMHudDelegate.h +++ /dev/null @@ -1,25 +0,0 @@ -/* - * ATMHudDelegate.h - * ATMHud - * - * Created by Marcel Müller on 2011-03-01. - * Copyright (c) 2010-2011, Marcel Müller (atomcraft) - * All rights reserved. - * - * https://github.com/atomton/ATMHud - */ - -@class ATMHud; - -@protocol ATMHudDelegate - -@optional -- (void)userDidTapHud:(ATMHud *)_hud; -- (void)hudWillAppear:(ATMHud *)_hud; -- (void)hudDidAppear:(ATMHud *)_hud; -- (void)hudWillUpdate:(ATMHud *)_hud; -- (void)hudDidUpdate:(ATMHud *)_hud; -- (void)hudWillDisappear:(ATMHud *)_hud; -- (void)hudDidDisappear:(ATMHud *)_hud; - -@end diff --git a/ATMHudView.m b/ATMHudView.m index dc8d679..0deafc3 100644 --- a/ATMHudView.m +++ b/ATMHudView.m @@ -14,7 +14,7 @@ #import "ATMProgressLayer.h" #import "ATMHud.h" #import -#import "ATMHudDelegate.h" +#import "ATMHudBlock.h" #import "ATMHudQueueItem.h" @@ -376,9 +376,9 @@ - (void)applyWithMode:(ATMHudApplyMode)mode { CGRect r = self.frame; [self setFrame:[self sharpRect:r]]; - if ([(id)p.delegate respondsToSelector:@selector(hudWillAppear:)]) { - [p.delegate hudWillAppear:p]; - } + if (p.block && p.block.willAppearBlock) { + p.block.willAppearBlock(p); + } self.transform = CGAffineTransformMakeScale(p.appearScaleFactor, p.appearScaleFactor); @@ -395,9 +395,9 @@ - (void)applyWithMode:(ATMHudApplyMode)mode { if (![p.showSound isEqualToString:@""] && p.showSound != NULL) { [p playSound:p.showSound]; } - if ([(id)p.delegate respondsToSelector:@selector(hudDidAppear:)]) { - [p.delegate hudDidAppear:p]; - } + if (p.block && p.block.didAppearBlock) { + p.block.didAppearBlock(p); + } } }]; }]; @@ -430,10 +430,10 @@ - (void)applyWithMode:(ATMHudApplyMode)mode { break; } - case ATMHudApplyModeUpdate: { - if ([(id)p.delegate respondsToSelector:@selector(hudWillUpdate:)]) { - [p.delegate hudWillUpdate:p]; - } + case ATMHudApplyModeUpdate: { + if (p.block && p.block.willUpdateBlock) { + p.block.willUpdateBlock(p); + } if (CGPointEqualToPoint(p.center, CGPointZero)) { self.frame = CGRectMake((self.superview.bounds.size.width-targetBounds.size.width)*0.5, (self.superview.bounds.size.height-targetBounds.size.height)*0.5, targetBounds.size.width, targetBounds.size.height); @@ -474,9 +474,9 @@ - (void)applyWithMode:(ATMHudApplyMode)mode { if (![p.updateSound isEqualToString:@""] && p.updateSound != NULL) { [p playSound:p.updateSound]; } - if ([(id)p.delegate respondsToSelector:@selector(hudDidUpdate:)]) { - [p.delegate hudDidUpdate:p]; - } + if (p.block && p.block.didUpdateBlock) { + p.block.didUpdateBlock(p); + } }]; backgroundLayer.position = CGPointMake(0.5*targetBounds.size.width, 0.5*targetBounds.size.height); @@ -500,9 +500,9 @@ - (void)applyWithMode:(ATMHudApplyMode)mode { } case ATMHudApplyModeHide: { - if ([(id)p.delegate respondsToSelector:@selector(hudWillDisappear:)]) { - [p.delegate hudWillDisappear:p]; - } + if (p.block && p.block.willDisappearBlock) { + p.block.willDisappearBlock(p); + } if (![p.hideSound isEqualToString:@""] && p.hideSound != NULL) { [p playSound:p.hideSound]; } @@ -517,9 +517,9 @@ - (void)applyWithMode:(ATMHudApplyMode)mode { self.superview.userInteractionEnabled = NO; self.transform = CGAffineTransformMakeScale(1.0, 1.0); [self reset]; - if ([(id)p.delegate respondsToSelector:@selector(hudDidDisappear:)]) { - [p.delegate hudDidDisappear:p]; - } + if (p.block && p.block.didDisappearBlock) { + p.block.didDisappearBlock(p); + } } }]; break; diff --git a/Classes/DemoViewController.h b/Classes/DemoViewController.h index 2066b93..cd593ee 100644 --- a/Classes/DemoViewController.h +++ b/Classes/DemoViewController.h @@ -21,10 +21,10 @@ */ #import -#import "ATMHudDelegate.h" + @class ATMHud; -@interface DemoViewController : UIViewController { +@interface DemoViewController : UIViewController { UITableView *tv_demo; ATMHud *hud; diff --git a/Classes/DemoViewController.m b/Classes/DemoViewController.m index 70de809..0e0097e 100644 --- a/Classes/DemoViewController.m +++ b/Classes/DemoViewController.m @@ -63,7 +63,9 @@ - (void)loadView { [baseView addSubview:tv_demo]; - hud = [[ATMHud alloc] initWithDelegate:self]; + hud = [[ATMHud alloc] initWithBlock:[ATMHudBlock blockWithUserDidTapHud:^(ATMHud *_hud) { + [_hud hide]; + } willAppear:nil didAppear:nil willUpdate:nil didUpdate:nil willDisappear:nil didDisappear:nil]]; [baseView addSubview:hud.view]; self.view = baseView; @@ -345,12 +347,6 @@ - (void)showNextDisplayInQueue { } } -#pragma mark - -#pragma mark ATMHudDelegate -- (void)userDidTapHud:(ATMHud *)_hud { - [_hud hide]; -} - #pragma mark - #pragma mark More examples