-
-
Save LutherBaker/cda6b51b6f4ed88ab2c5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// | |
// CustomDynamicAlertView.h | |
// Week3_inclass | |
// | |
// Created by Victor Baro on 18/02/2015. | |
// Copyright (c) 2015 Produkt. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@protocol CustomDynamicAlertViewDelegate <NSObject> | |
- (void)dismissAnimationFinished; | |
@end | |
@interface CustomDynamicAlertView : UIView | |
@property (nonatomic, strong) NSString *mainText; | |
@property (nonatomic, strong) NSString *dismissButtonText; | |
@property (nonatomic, weak) id<CustomDynamicAlertViewDelegate> delegate; | |
- (instancetype)initWithFrame:(CGRect)frame | |
mainText:(NSString *)mainText | |
dismissButtonText:(NSString *)dismissButtonText; | |
@end | |
// | |
// CustomDynamicAlertView.m | |
// Week3_inclass | |
// | |
// Created by Victor Baro on 18/02/2015. | |
// Copyright (c) 2015 Produkt. All rights reserved. | |
// | |
#import "CustomDynamicAlertView.h" | |
@interface CustomDynamicAlertView () <UICollisionBehaviorDelegate> | |
@property (nonatomic,weak) UIView *alertView; | |
@property (nonatomic, weak) UILabel *mainTextLabel; | |
@property (nonatomic, weak) UIButton *dismissButton; | |
@property (nonatomic, strong) UIDynamicAnimator *mainAnimator; | |
@property (nonatomic, strong) UISnapBehavior *snapBehavior; | |
@end | |
static CGFloat const buttonHeight = 50; | |
static CGFloat const alertWidth = 250; | |
static CGFloat const alertHeight = 150; | |
static CGFloat const mainTextMargin = 15; | |
static NSString const *kBoundaryID = @"outsideBoundary"; | |
@implementation CustomDynamicAlertView | |
- (instancetype)initWithFrame:(CGRect)frame { | |
return [self initWithFrame:frame mainText:@"Add a main text to show here as a main text." dismissButtonText:@"Dismiss"]; | |
} | |
- (instancetype)initWithFrame:(CGRect)frame mainText:(NSString *)mainText dismissButtonText:(NSString *)dismissButtonText { | |
self = [super initWithFrame:frame]; | |
if (self) { | |
_mainText = mainText; | |
_dismissButtonText = dismissButtonText; | |
[self initializeCustomProperties]; | |
} | |
return self; | |
} | |
- (void)initializeCustomProperties { | |
self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.6]; | |
[self createAlertView]; | |
[self initializeDynamics]; | |
} | |
- (void)createAlertView { | |
UIView *alertView = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.bounds)/2 - alertWidth/2, | |
-400, | |
alertWidth, | |
alertHeight)]; | |
alertView.backgroundColor = [UIColor whiteColor]; | |
alertView.clipsToBounds = YES; | |
alertView.layer.cornerRadius = 10; | |
[self addSubview:alertView]; | |
self.alertView = alertView; | |
UILabel *mainTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(mainTextMargin, 0, alertWidth - mainTextMargin*2, alertHeight - buttonHeight)]; | |
mainTextLabel.text = self.mainText; | |
mainTextLabel.backgroundColor = [UIColor clearColor]; | |
mainTextLabel.textColor = [UIColor colorWithWhite:0.05 alpha:1.0]; | |
mainTextLabel.font = [UIFont systemFontOfSize:14]; | |
mainTextLabel.numberOfLines = 0; | |
mainTextLabel.textAlignment = NSTextAlignmentCenter; | |
[alertView addSubview:mainTextLabel]; | |
self.mainTextLabel = mainTextLabel; | |
UIButton *dismissButton = [UIButton buttonWithType:UIButtonTypeSystem]; | |
dismissButton.frame = CGRectMake(0, alertHeight - buttonHeight, alertWidth, buttonHeight); | |
[dismissButton setTitle:self.dismissButtonText forState:UIControlStateNormal]; | |
[dismissButton addTarget:self action:@selector(dismissButtonPressed) forControlEvents:UIControlEventTouchUpInside]; | |
dismissButton.backgroundColor = [UIColor redColor]; | |
dismissButton.tintColor = [UIColor whiteColor]; | |
[alertView addSubview:dismissButton]; | |
self.dismissButton = dismissButton; | |
} | |
- (void)initializeDynamics { | |
self.mainAnimator = [[UIDynamicAnimator alloc]initWithReferenceView:self]; | |
self.snapBehavior = [[UISnapBehavior alloc]initWithItem:self.alertView snapToPoint:CGPointMake(CGRectGetWidth(self.bounds)/2, CGRectGetHeight(self.bounds)/2)]; | |
[self.mainAnimator addBehavior:self.snapBehavior]; | |
} | |
- (void)dismissButtonPressed { | |
[self.mainAnimator removeAllBehaviors]; | |
UIGravityBehavior *gravityB = [[UIGravityBehavior alloc]initWithItems:@[self.alertView]]; | |
gravityB.magnitude = 4.0; | |
[self.mainAnimator addBehavior:gravityB]; | |
UIPushBehavior *pushB = [[UIPushBehavior alloc]initWithItems:@[self.alertView] mode:UIPushBehaviorModeInstantaneous]; | |
[pushB setAngle:-(M_PI_2 * 0.8) magnitude:25.0]; | |
[self.mainAnimator addBehavior:pushB]; | |
UICollisionBehavior *collisionB = [[UICollisionBehavior alloc]initWithItems:@[self.alertView]]; | |
[collisionB addBoundaryWithIdentifier:kBoundaryID | |
fromPoint:CGPointMake(0, CGRectGetHeight(self.bounds) + alertHeight) | |
toPoint:CGPointMake(CGRectGetWidth(self.bounds) * 2, CGRectGetHeight(self.bounds) + alertHeight)]; | |
collisionB.collisionDelegate = self; | |
[self.mainAnimator addBehavior:collisionB]; | |
} | |
#pragma mark - CollisionDelegate | |
- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying>)identifier atPoint:(CGPoint)p { | |
if (identifier == kBoundaryID) { | |
[self.delegate dismissAnimationFinished]; | |
[self removeFromSuperview]; | |
} | |
} | |
#pragma mark - setters | |
- (void)setMainText:(NSString *)mainText { | |
_mainText = mainText; | |
self.mainTextLabel.text = mainText; | |
} | |
- (void)setDismissButtonText:(NSString *)dismissButtonText { | |
_dismissButtonText = dismissButtonText; | |
[self.dismissButton setTitle:dismissButtonText forState:UIControlStateNormal]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment