Skip to content

Instantly share code, notes, and snippets.

@victorBaro
Created February 23, 2015 09:52
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • Save victorBaro/b9fcb4aee888f6140daf to your computer and use it in GitHub Desktop.
Save victorBaro/b9fcb4aee888f6140daf to your computer and use it in GitHub Desktop.
Small class (Alert View) we made during third week at ironhack bootcamp using UIKit Dynamics. http://victorbaro.com/2015/02/custom-dynamic-alertview/
//
// 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