Permalink
Browse files

Merge pull request #7 from mcho2137/master

Add delegate methods
  • Loading branch information...
2 parents 17421fc + 15f879a commit 04feef2a64117c2a6c4dd21db51e42d1425ea649 @jberlana committed Aug 3, 2012
View
BIN .DS_Store
Binary file not shown.
@@ -25,7 +25,7 @@
#import <UIKit/UIKit.h>
#import "JBKenBurnsView.h"
-@interface ExampleViewController : UIViewController {
+@interface ExampleViewController : UIViewController <KenBurnsViewDelegate> {
KenBurnsView *kenView;
}
@@ -44,7 +44,8 @@ - (void)viewDidLoad
[super viewDidLoad];
self.kenView.layer.borderWidth = 1;
- self.kenView.layer.borderColor = [UIColor blackColor].CGColor;
+ self.kenView.layer.borderColor = [UIColor blackColor].CGColor;
+ self.kenView.delegate = self;
NSArray *myImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"image1.jpeg"],
@@ -62,6 +63,7 @@ - (void)viewDidLoad
- (void)viewDidUnload
{
+ self.kenView.delegate = nil;
[self setKenView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
@@ -88,4 +90,16 @@ - (void)dealloc {
[super dealloc];
}
+#pragma KenBurnsViewDelegate
+- (void)didShowImageAtIndex:(NSUInteger)index
+{
+ NSLog(@"Finished image: %d", index);
+}
+
+- (void)didFinishAllAnimations
+{
+ NSLog(@"Yay all done!");
+}
+
+
@end
View
@@ -26,19 +26,33 @@
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
+@class KenBurnsView;
+
+#pragma - KenBurnsViewDelegate
+@protocol KenBurnsViewDelegate <NSObject>
+@optional
+- (void)didShowImageAtIndex:(NSUInteger)index;
+- (void)didFinishAllAnimations;
+
+@end
+
@interface KenBurnsView : UIView {
NSMutableArray *imagesArray;
float timeTransition;
BOOL isLoop;
BOOL isLandscape;
+ __weak id <KenBurnsViewDelegate> delegate;
}
@property (nonatomic, assign) float timeTransition;
@property (nonatomic, retain) NSMutableArray *imagesArray;
@property (nonatomic) BOOL isLoop;
@property (nonatomic) BOOL isLandscape;
+@property (weak) id<KenBurnsViewDelegate> delegate;
- (void) animateWithImages:(NSArray *)images transitionDuration:(float)time loop:(BOOL)isLoop isLandscape:(BOOL)isLandscape;
- (void) animateWithURLs:(NSArray *)urls transitionDuration:(float)duration loop:(BOOL)shouldLoop isLandscape:(BOOL)inLandscape;
@end
+
+
View
@@ -31,20 +31,21 @@
// Private interface
@interface KenBurnsView ()
- @property (nonatomic) int currentImage;
- @property (nonatomic) BOOL animationInCurse;
+@property (nonatomic) int currentImage;
+@property (nonatomic) BOOL animationInCurse;
- - (void) _animate:(NSNumber*)num;
- - (void) _startAnimations:(NSArray*)images;
- - (void) _startInternetAnimations:(NSArray *)urls;
- - (UIImage *) _downloadImageFrom:(NSString *)url;
+- (void) _animate:(NSNumber*)num;
+- (void) _startAnimations:(NSArray*)images;
+- (void) _startInternetAnimations:(NSArray *)urls;
+- (UIImage *) _downloadImageFrom:(NSString *)url;
+- (void) _notifyDelegate:(NSNumber *) imageIndex;
@end
@implementation KenBurnsView
@synthesize imagesArray, timeTransition, isLoop, isLandscape;
-@synthesize animationInCurse, currentImage;
+@synthesize animationInCurse, currentImage, delegate;
-(id)init
@@ -76,7 +77,7 @@ - (void) animateWithImages:(NSMutableArray *)images transitionDuration:(float)du
self.layer.masksToBounds = YES;
[NSThread detachNewThreadSelector:@selector(_startAnimations:) toTarget:self withObject:images];
-
+
}
- (void) animateWithURLs:(NSArray *)urls transitionDuration:(float)duration loop:(BOOL)shouldLoop isLandscape:(BOOL)inLandscape;
@@ -113,9 +114,11 @@ - (void) _startAnimations:(NSArray *)images
waitUntilDone:YES];
sleep(self.timeTransition);
- i = (i == [images count]-1) && isLoop ? -1 : i;
+
+ i = (i == [images count] - 1) && isLoop ? -1 : i;
+
}
-
+
[pool release];
}
@@ -133,7 +136,7 @@ - (void) _startInternetAnimations:(NSArray *)urls
int bufferIndex = 0;
for (int urlIndex=self.imagesArray.count; urlIndex < [urls count]; urlIndex++) {
-
+
[self performSelectorOnMainThread:@selector(_animate:)
withObject:[NSNumber numberWithInt:0]
waitUntilDone:YES];
@@ -189,7 +192,7 @@ - (void) _animate:(NSNumber*)num
else
resizeRatio = frameWidth / image.size.width;
- // No higher than screen
+ // No higher than screen
}
else
{
@@ -201,7 +204,7 @@ - (void) _animate:(NSNumber*)num
resizeRatio = self.bounds.size.height / image.size.height;
}
- // No widder than screen
+ // No widder than screen
}
else
{
@@ -217,7 +220,7 @@ - (void) _animate:(NSNumber*)num
else
resizeRatio = frameWidth / image.size.width;
- // No higher than screen
+ // No higher than screen
}
else
{
@@ -240,7 +243,7 @@ - (void) _animate:(NSNumber*)num
float maxMoveY = optimusHeight - frameHeight;
float rotation = (arc4random() % 9) / 100;
-
+
switch (arc4random() % 4) {
case 0:
originX = 0;
@@ -259,7 +262,7 @@ - (void) _animate:(NSNumber*)num
moveX = -maxMoveX;
moveY = maxMoveY;
break;
-
+
case 2:
originX = frameWidth - optimusWidth;
@@ -316,8 +319,25 @@ - (void) _animate:(NSNumber*)num
imageView.transform = transform;
[UIView commitAnimations];
+ [self performSelector:@selector(_notifyDelegate:) withObject:num afterDelay:self.timeTransition];
+
[imageView release];
}
+- (void) _notifyDelegate: (NSNumber *)imageIndex
+{
+ if (delegate) {
+ if([self.delegate respondsToSelector:@selector(didShowImageAtIndex:)])
+ {
+ [self.delegate didShowImageAtIndex:[imageIndex intValue]];
+ }
+
+ if ([imageIndex intValue] == ([self.imagesArray count]-1) && !isLoop && [self.delegate respondsToSelector:@selector(didFinishAllAnimations)]) {
+ [self.delegate didFinishAllAnimations];
+ }
+ }
+
+}
+
@end
@@ -0,0 +1,85 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Scheme
+ version = "1.3">
+ <BuildAction
+ parallelizeBuildables = "YES"
+ buildImplicitDependencies = "YES">
+ <BuildActionEntries>
+ <BuildActionEntry
+ buildForTesting = "YES"
+ buildForRunning = "YES"
+ buildForProfiling = "YES"
+ buildForArchiving = "YES"
+ buildForAnalyzing = "YES">
+ <BuildableReference
+ BuildableIdentifier = "primary"
+ BlueprintIdentifier = "42B17962142CCBE3006D5610"
+ BuildableName = "KenBurns.app"
+ BlueprintName = "KenBurns"
+ ReferencedContainer = "container:KenBurnsDemo.xcodeproj">
+ </BuildableReference>
+ </BuildActionEntry>
+ </BuildActionEntries>
+ </BuildAction>
+ <TestAction
+ selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
+ selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
+ shouldUseLaunchSchemeArgsEnv = "YES"
+ buildConfiguration = "Debug">
+ <Testables>
+ </Testables>
+ <MacroExpansion>
+ <BuildableReference
+ BuildableIdentifier = "primary"
+ BlueprintIdentifier = "42B17962142CCBE3006D5610"
+ BuildableName = "KenBurns.app"
+ BlueprintName = "KenBurns"
+ ReferencedContainer = "container:KenBurnsDemo.xcodeproj">
+ </BuildableReference>
+ </MacroExpansion>
+ </TestAction>
+ <LaunchAction
+ selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
+ selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
+ launchStyle = "0"
+ useCustomWorkingDirectory = "NO"
+ buildConfiguration = "Debug"
+ ignoresPersistentStateOnLaunch = "NO"
+ debugDocumentVersioning = "YES"
+ allowLocationSimulation = "YES">
+ <BuildableProductRunnable>
+ <BuildableReference
+ BuildableIdentifier = "primary"
+ BlueprintIdentifier = "42B17962142CCBE3006D5610"
+ BuildableName = "KenBurns.app"
+ BlueprintName = "KenBurns"
+ ReferencedContainer = "container:KenBurnsDemo.xcodeproj">
+ </BuildableReference>
+ </BuildableProductRunnable>
+ <AdditionalOptions>
+ </AdditionalOptions>
+ </LaunchAction>
+ <ProfileAction
+ shouldUseLaunchSchemeArgsEnv = "YES"
+ savedToolIdentifier = ""
+ useCustomWorkingDirectory = "NO"
+ buildConfiguration = "Release"
+ debugDocumentVersioning = "YES">
+ <BuildableProductRunnable>
+ <BuildableReference
+ BuildableIdentifier = "primary"
+ BlueprintIdentifier = "42B17962142CCBE3006D5610"
+ BuildableName = "KenBurns.app"
+ BlueprintName = "KenBurns"
+ ReferencedContainer = "container:KenBurnsDemo.xcodeproj">
+ </BuildableReference>
+ </BuildableProductRunnable>
+ </ProfileAction>
+ <AnalyzeAction
+ buildConfiguration = "Debug">
+ </AnalyzeAction>
+ <ArchiveAction
+ buildConfiguration = "Release"
+ revealArchiveInOrganizer = "YES">
+ </ArchiveAction>
+</Scheme>
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>SchemeUserState</key>
+ <dict>
+ <key>KenBurns.xcscheme</key>
+ <dict>
+ <key>orderHint</key>
+ <integer>0</integer>
+ </dict>
+ </dict>
+ <key>SuppressBuildableAutocreation</key>
+ <dict>
+ <key>42B17962142CCBE3006D5610</key>
+ <dict>
+ <key>primary</key>
+ <true/>
+ </dict>
+ </dict>
+</dict>
+</plist>

0 comments on commit 04feef2

Please sign in to comment.