Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scrollview example with proper inset treatment
- Loading branch information
Showing
7 changed files
with
107 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#import <Foundation/Foundation.h> | ||
|
||
// | ||
// This controller demonstrates how to use SMCalloutView with a basic UIScrollView. | ||
// | ||
|
||
@interface ScrollViewController : UIViewController | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.