Skip to content

Commit

Permalink
Scrollview example with proper inset treatment
Browse files Browse the repository at this point in the history
  • Loading branch information
nfarina committed Feb 13, 2014
1 parent e0a560e commit f13a735
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 11 deletions.
7 changes: 6 additions & 1 deletion SampleProject/AppDelegate.m
@@ -1,16 +1,21 @@
#import "AppDelegate.h"
#import "ScrollViewController.h"
#import "MapKitComparisonController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// this tab demonstrates
// this tab demonstrates how to use SMCalloutView with UIScrollView
ScrollViewController *scrollViewController = [ScrollViewController new];

// this tab demonstrates how to use SMCalloutView with MKMapView
MapKitComparisonController *mapKitController = [MapKitComparisonController new];

// wrap it all up in a tab bar controller
self.tabBarController = [UITabBarController new];
self.tabBarController.viewControllers = @[
[[UINavigationController alloc] initWithRootViewController:scrollViewController],
[[UINavigationController alloc] initWithRootViewController:mapKitController]];

// create the main window and display it
Expand Down
14 changes: 10 additions & 4 deletions SampleProject/CalloutViewSamples.xcodeproj/project.pbxproj
Expand Up @@ -11,8 +11,9 @@
011CA09015D3144E007DBC54 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 011CA08F15D3144E007DBC54 /* Foundation.framework */; };
011CA09215D3144E007DBC54 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 011CA09115D3144E007DBC54 /* CoreGraphics.framework */; };
0145C74115D3182F007244F3 /* MapKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0145C74015D3182F007244F3 /* MapKit.framework */; };
0145C74615D32505007244F3 /* mars.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 0145C74515D32505007244F3 /* mars.jpg */; };
0148A58218AC303100463FE9 /* SMClassicCalloutView.m in Sources */ = {isa = PBXBuildFile; fileRef = 0148A58118AC303100463FE9 /* SMClassicCalloutView.m */; };
0148A58518AD37F400463FE9 /* ScrollViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 0148A58418AD37F400463FE9 /* ScrollViewController.m */; };
0148A58718AD390D00463FE9 /* mars@2x.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 0148A58618AD390D00463FE9 /* mars@2x.jpg */; };
017FD73F15D317B1006825B3 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 017FD73D15D317A5006825B3 /* AppDelegate.m */; };
017FD74015D317B1006825B3 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 017FD73C15D3162C006825B3 /* main.m */; };
01A1575918AAFFA900A93F0E /* MapKitComparisonController.m in Sources */ = {isa = PBXBuildFile; fileRef = 01A1575818AAFFA900A93F0E /* MapKitComparisonController.m */; };
Expand All @@ -27,9 +28,11 @@
011CA08F15D3144E007DBC54 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
011CA09115D3144E007DBC54 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
0145C74015D3182F007244F3 /* MapKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MapKit.framework; path = System/Library/Frameworks/MapKit.framework; sourceTree = SDKROOT; };
0145C74515D32505007244F3 /* mars.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = mars.jpg; sourceTree = SOURCE_ROOT; };
0148A58018AC303100463FE9 /* SMClassicCalloutView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SMClassicCalloutView.h; path = ../SMClassicCalloutView.h; sourceTree = "<group>"; };
0148A58118AC303100463FE9 /* SMClassicCalloutView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SMClassicCalloutView.m; path = ../SMClassicCalloutView.m; sourceTree = "<group>"; };
0148A58318AD37F400463FE9 /* ScrollViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ScrollViewController.h; sourceTree = SOURCE_ROOT; };
0148A58418AD37F400463FE9 /* ScrollViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ScrollViewController.m; sourceTree = SOURCE_ROOT; };
0148A58618AD390D00463FE9 /* mars@2x.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = "mars@2x.jpg"; sourceTree = SOURCE_ROOT; };
017FD73A15D3162C006825B3 /* CalloutViewSamples-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "CalloutViewSamples-Info.plist"; sourceTree = SOURCE_ROOT; };
017FD73B15D3162C006825B3 /* CalloutViewSamples-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "CalloutViewSamples-Prefix.pch"; sourceTree = SOURCE_ROOT; };
017FD73C15D3162C006825B3 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -94,6 +97,8 @@
children = (
017FD73E15D317A5006825B3 /* AppDelegate.h */,
017FD73D15D317A5006825B3 /* AppDelegate.m */,
0148A58318AD37F400463FE9 /* ScrollViewController.h */,
0148A58418AD37F400463FE9 /* ScrollViewController.m */,
01A1575718AAFFA900A93F0E /* MapKitComparisonController.h */,
01A1575818AAFFA900A93F0E /* MapKitComparisonController.m */,
017FD73915D31618006825B3 /* Supporting Files */,
Expand All @@ -119,7 +124,7 @@
017FD73A15D3162C006825B3 /* CalloutViewSamples-Info.plist */,
017FD73B15D3162C006825B3 /* CalloutViewSamples-Prefix.pch */,
017FD73C15D3162C006825B3 /* main.m */,
0145C74515D32505007244F3 /* mars.jpg */,
0148A58618AD390D00463FE9 /* mars@2x.jpg */,
01A1D513161129080021F8DD /* Default-568h@2x.png */,
);
name = "Supporting Files";
Expand Down Expand Up @@ -175,7 +180,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
0145C74615D32505007244F3 /* mars.jpg in Resources */,
0148A58718AD390D00463FE9 /* mars@2x.jpg in Resources */,
01A1D514161129080021F8DD /* Default-568h@2x.png in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -187,6 +192,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
0148A58518AD37F400463FE9 /* ScrollViewController.m in Sources */,
0148A58218AC303100463FE9 /* SMClassicCalloutView.m in Sources */,
01A1575918AAFFA900A93F0E /* MapKitComparisonController.m in Sources */,
01A6EE6515D49BD500E895D6 /* SMCalloutView.m in Sources */,
Expand Down
14 changes: 8 additions & 6 deletions SampleProject/MapKitComparisonController.m
Expand Up @@ -49,8 +49,6 @@ - (void)viewDidLoad {
// create our custom callout view
self.calloutView = [SMCalloutView platformCalloutView];
self.calloutView.delegate = self;
self.calloutView.title = @"Cape Canaveral";
self.calloutView.subtitle = @"Launchpad";

UIButton *topDisclosure = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[topDisclosure addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disclosureTapped)]];
Expand Down Expand Up @@ -91,19 +89,23 @@ - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnot
return view;
}

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)annotationView {

if (mapView == self.mapKitWithSMCalloutView) {


// apply the MKAnnotationView's basic properties
self.calloutView.title = annotationView.annotation.title;
self.calloutView.subtitle = annotationView.annotation.subtitle;

// Apply the MKAnnotationView's desired calloutOffset (from the top-middle of the view)
self.calloutView.calloutOffset = view.calloutOffset;
self.calloutView.calloutOffset = annotationView.calloutOffset;

// iOS 7 only: Apply our view controller's edge insets to the allowable area in which the callout can be displayed.
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
self.calloutView.constrainedInsets = UIEdgeInsetsMake(self.topLayoutGuide.length, 0, self.bottomLayoutGuide.length, 0);

// This does all the magic.
[self.calloutView presentCalloutFromRect:view.bounds inView:view constrainedToView:self.view animated:YES];
[self.calloutView presentCalloutFromRect:annotationView.bounds inView:annotationView constrainedToView:self.view animated:YES];
}
}

Expand Down
9 changes: 9 additions & 0 deletions SampleProject/ScrollViewController.h
@@ -0,0 +1,9 @@
#import <Foundation/Foundation.h>

//
// This controller demonstrates how to use SMCalloutView with a basic UIScrollView.
//

@interface ScrollViewController : UIViewController

@end
74 changes: 74 additions & 0 deletions SampleProject/ScrollViewController.m
@@ -0,0 +1,74 @@
#import "ScrollViewController.h"
#import <MapKit/MapKit.h>
#import "SMCalloutView.h"

@interface ScrollViewController () <UIGestureRecognizerDelegate, SMCalloutViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIImageView *marsView;
@property (nonatomic, strong) MKPinAnnotationView *pinView;
@property (nonatomic, strong) SMCalloutView *calloutView;
@end

@implementation ScrollViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nil bundle:nil]) {

self.title = @"ScrollView";
}
return self;
}

- (void)loadView {

self.marsView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mars.jpg"]];
self.marsView.userInteractionEnabled = YES;
[self.marsView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(marsTapped)]];

self.scrollView = [UIScrollView new];
self.scrollView.contentSize = self.marsView.image.size;
self.scrollView.contentOffset = CGPointMake(40, 40);
self.scrollView.bounces = NO;
[self.scrollView addSubview:self.marsView];

self.pinView = [[MKPinAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:@""];
self.pinView.center = CGPointMake(230, 200);
[self.pinView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pinTapped)]];
[self.marsView addSubview:self.pinView];

self.calloutView = [SMCalloutView new];
self.calloutView.delegate = self;
self.calloutView.title = @"Curiosity";

self.view = self.scrollView;
}

- (void)pinTapped {

// Apply the MKAnnotationView's desired calloutOffset (from the top-middle of the view)
self.calloutView.calloutOffset = self.pinView.calloutOffset;

// Apply any scroll view edge insets
self.calloutView.constrainedInsets = self.scrollView.contentInset;

// This does all the magic.
[self.calloutView presentCalloutFromRect:self.pinView.bounds inView:self.pinView constrainedToView:self.view animated:YES];
}

- (NSTimeInterval)calloutView:(SMCalloutView *)calloutView delayForRepositionWithSize:(CGSize)offset {

// the callout is telling us we need to move the scroll view such that the callout will be completely visible when it appears.
CGPoint contentOffset = self.scrollView.contentOffset;
contentOffset.x -= offset.width;
contentOffset.y -= offset.height;

[self.scrollView setContentOffset:contentOffset animated:YES];

return kSMCalloutViewRepositionDelayForUIScrollView;
}

- (void)marsTapped {
[self.calloutView dismissCalloutAnimated:YES];
}

@end
Binary file removed SampleProject/mars.jpg
Binary file not shown.
Binary file added SampleProject/mars@2x.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f13a735

Please sign in to comment.