Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an optional performCustomAnimations method in CKClusterManagerD… #80

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion ClusterKit/Core/CKCluster.h
Expand Up @@ -65,7 +65,7 @@ MK_EXTERN NSComparisonResult MKMapSizeCompare(MKMapSize size1, MKMapSize size2);
/**
Cluster coordinate.
*/
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic) CLLocationCoordinate2D coordinate;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this change is required as CKCluster implements the MKAnnotation method setCoordinate:.


/**
Cluster annotation array.
Expand Down
29 changes: 27 additions & 2 deletions ClusterKit/Core/CKClusterManager.h
Expand Up @@ -31,6 +31,7 @@ FOUNDATION_EXTERN const double kCKMarginFactorWorld;

@protocol CKMap;
@class CKClusterManager;
@class CKClusterAnimation;

/**
The delegate of a CKClusterManager object may adopt the CKClusterManagerDelegate protocol.
Expand All @@ -50,6 +51,15 @@ FOUNDATION_EXTERN const double kCKMarginFactorWorld;
*/
- (BOOL)clusterManager:(CKClusterManager *)clusterManager shouldClusterAnnotation:(id<MKAnnotation>)annotation;

/**
Tells the delegate to perform a custom animation based on the animations array.

@param clusterManager The cluster manager object requesting the animation.
@param customAnimations An array of CKClusterAnimation objects containing the animations. This parameter must not be NULL.
@param completion A block object to be executed when the animation sequence ends. This block has no return value and takes a single Boolean argument that indicates whether or not the animations actually finished before the completion handler was called. If the duration of the animation is 0, this block is performed at the beginning of the next run loop cycle. This parameter may be NULL.
*/
- (void)clusterManager:(CKClusterManager *)clusterManager performCustomAnimations:(NSArray <CKClusterAnimation *> *)animations completion:(void (^ __nullable)(BOOL finished))completion;

/**
Tells the delegate to perform an animation.

Expand Down Expand Up @@ -169,6 +179,14 @@ FOUNDATION_EXTERN const double kCKMarginFactorWorld;

@end

/**
CKClusterAnimationType defines the type of the CKClusterAnimation object.
*/
typedef NS_ENUM(NSInteger, CKClusterAnimationType) {
CKClusterAnimationTypeCollapse,
CKClusterAnimationTypeExpand
};

/**
CKClusterAnimation defines a cluster animation from a start coordinate to an end coordinate on a map.
*/
Expand All @@ -189,25 +207,32 @@ FOUNDATION_EXTERN const double kCKMarginFactorWorld;
*/
@property (nonatomic) CLLocationCoordinate2D to;

/**
The type of the animation.
*/
@property (nonatomic) CKClusterAnimationType type;

/**
Initializes an animation for the given cluster.

@param cluster The cluster to animate.
@param from The cluster starting point.
@param to The cluster ending point.
@param type The type of the animation.
@return The initialized CKClusterAnimation object.
*/
- (instancetype)initWithCluster:(CKCluster *)cluster from:(CLLocationCoordinate2D)from to:(CLLocationCoordinate2D)to NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithCluster:(CKCluster *)cluster from:(CLLocationCoordinate2D)from to:(CLLocationCoordinate2D)to type:(CKClusterAnimationType)type NS_DESIGNATED_INITIALIZER;

/**
Creates an animation for the given cluster.

@param cluster The cluster to animate.
@param from The cluster starting point.
@param to The cluster ending point.
@param type The type of the animation.
@return The initialized CKClusterAnimation object.
*/
+ (instancetype)animateCluster:(CKCluster *)cluster from:(CLLocationCoordinate2D)from to:(CLLocationCoordinate2D)to;
+ (instancetype)animateCluster:(CKCluster *)cluster from:(CLLocationCoordinate2D)from to:(CLLocationCoordinate2D)to type:(CKClusterAnimationType)type;

/// :nodoc:
- (instancetype)init NS_UNAVAILABLE;
Expand Down
11 changes: 6 additions & 5 deletions ClusterKit/Core/CKClusterManager.m
Expand Up @@ -254,7 +254,7 @@ - (void)expand:(NSArray<CKCluster *> *)newClusters from:(NSArray<CKCluster *> *)
CKClusterAnimation *animation = [animations member:neighbor];

if (!animation) {
animation = [CKClusterAnimation animateCluster:neighbor from:oldCluster.coordinate to:neighbor.coordinate];
animation = [CKClusterAnimation animateCluster:neighbor from:oldCluster.coordinate to:neighbor.coordinate type:CKClusterAnimationTypeExpand];
[animations addObject:animation];
continue;
}
Expand Down Expand Up @@ -290,7 +290,7 @@ - (void)collapse:(NSArray<CKCluster *> *)oldClusters to:(NSArray<CKCluster *> *)
CKClusterAnimation *animation = [animations member:neighbor];

if (!animation) {
animation = [CKClusterAnimation animateCluster:neighbor from:neighbor.coordinate to:newCluster.coordinate];
animation = [CKClusterAnimation animateCluster:neighbor from:neighbor.coordinate to:newCluster.coordinate type:CKClusterAnimationTypeCollapse];
[animations addObject:animation];
continue;
}
Expand Down Expand Up @@ -325,16 +325,17 @@ - (BOOL)annotationTree:(id<CKAnnotationTree>)annotationTree shouldExtractAnnotat

@implementation CKClusterAnimation

+ (instancetype)animateCluster:(CKCluster *)cluster from:(CLLocationCoordinate2D)from to:(CLLocationCoordinate2D)to {
return [[self alloc] initWithCluster:cluster from:from to:to];
+ (instancetype)animateCluster:(CKCluster *)cluster from:(CLLocationCoordinate2D)from to:(CLLocationCoordinate2D)to type:(CKClusterAnimationType)type {
return [[self alloc] initWithCluster:cluster from:from to:to type:type];
}

- (instancetype)initWithCluster:(CKCluster *)cluster from:(CLLocationCoordinate2D)from to:(CLLocationCoordinate2D)to {
- (instancetype)initWithCluster:(CKCluster *)cluster from:(CLLocationCoordinate2D)from to:(CLLocationCoordinate2D)to type:(CKClusterAnimationType)type {
self = [super init];
if (self) {
_cluster = cluster;
_from = from;
_to = to;
_type = type;
}
return self;
}
Expand Down
8 changes: 7 additions & 1 deletion ClusterKit/Mapbox/MGLMapView+ClusterKit.m
Expand Up @@ -138,7 +138,13 @@ - (void)performAnimations:(NSArray<CKClusterAnimation *> *)animations completion
};
}

if ([self.clusterManager.delegate respondsToSelector:@selector(clusterManager:performAnimations:completion:)]) {
if ([self.clusterManager.delegate respondsToSelector:@selector(clusterManager:performCustomAnimations:completion:)]) {
[self.clusterManager.delegate clusterManager:self.clusterManager
performCustomAnimations:animations
completion:^(BOOL finished) {
if (completion) completion(finished);
}];
} else if ([self.clusterManager.delegate respondsToSelector:@selector(clusterManager:performAnimations:completion:)]) {
[self.clusterManager.delegate clusterManager:self.clusterManager
performAnimations:animationsBlock
completion:^(BOOL finished) {
Expand Down