I am trying to implement ,long press and delete functionality of apple, myself.
I almost achieved but, there is something i could not figure out.
When shaking animation starts delete button do not take the touches.
How to handle while it is shaking deleting the view.
Here is my code;
self.frame = CGRectMake(0, 0, 1024, 768);
[self setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.8]];
circleView = [[UIView alloc] initWithFrame:CGRectMake(50,55,90,90)];
circleView.layer.cornerRadius = 45;
circleView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1];
circleView.layer.borderColor = [[UIColor blackColor] CGColor];
circleView.layer.borderWidth = 4;
UILabel* circleIndex = [[UILabel alloc] init];
circleIndex.frame = CGRectMake(30, 25, 40, 40);
[circleIndex setFont:[UIFont fontWithName:#"Arial-BoldMT" size:40]];
[circleIndex setText:#"A"];
[circleView addSubview:circleIndex];
UIView *exitView = [[UIView alloc] initWithFrame:CGRectMake(78,5,20,20)];
exitView.layer.cornerRadius = 10;
exitView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:1];
exitView.layer.borderColor = [[UIColor whiteColor] CGColor];
exitView.layer.borderWidth = 2;
UILabel* exitLabel = [[UILabel alloc] init];
exitLabel.frame = CGRectMake(5, 0.5, 25, 20);
[exitLabel setFont:[UIFont fontWithName:#"Arial-BoldMT" size:25]];
[exitLabel setTextColor:[UIColor whiteColor]];
[exitLabel setBackgroundColor:[UIColor clearColor]];
[exitLabel setText:#"-"];
[exitView addSubview:exitLabel];
[circleView addSubview:exitView];
UIView *alertView = [[UIView alloc]initWithFrame:CGRectMake(40, 80, 944, 600)];
[exitView setUserInteractionEnabled:YES];
UILongPressGestureRecognizer *longPress =
[[UILongPressGestureRecognizer alloc] initWithTarget:self
action:#selector(handleLongPress:)];
[circleView addGestureRecognizer:longPress];
UITapGestureRecognizer *singlePress =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSinglePress:)];
[exitView addGestureRecognizer:singlePress];
//[longPress release];
[circleView bringSubviewToFront:exitView];
[alertView addSubview:circleView];
[self addSubview:alertView];
}
return self;
}
//The event handling method
- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer{
//CGPoint location = [recognizer locationInView:[recognizer.view superview]];
if ( recognizer.state == UIGestureRecognizerStateEnded ) {
//[circleView removeFromSuperview];
[self shakeView:recognizer.view];
}
}
- (void)handleSinglePress:(UITapGestureRecognizer *)recognizer{
//CGPoint location = [recognizer locationInView:[recognizer.view superview]];
if ( recognizer.state == UIGestureRecognizerStateEnded ) {
[recognizer.view.superview removeFromSuperview];
}
}
- (void)shakeView:(UIView *)viewToShake
{
CGFloat t = 2.0;
CGAffineTransform translateRight = CGAffineTransformTranslate(CGAffineTransformIdentity, t, 0.0);
CGAffineTransform translateLeft = CGAffineTransformTranslate(CGAffineTransformIdentity, -t, 0.0);
viewToShake.transform = translateLeft;
[UIView animateWithDuration:0.07 delay:0.0 options:UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeat animations:^{
[UIView setAnimationRepeatCount:200.0];
viewToShake.transform = translateRight;
} completion:^(BOOL finished) {
if (finished) {
[UIView animateWithDuration:0.05 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
viewToShake.transform = CGAffineTransformIdentity;
} completion:NULL];
}
}];
}
Use in the options parameter of your animation UIViewAnimationOptionAllowUserInteraction
Related
I wanted to have a blurred background and view popups in my application. But blur background and view is not appearing when I clicked my button.... can anyone please help me to achieve this task...
My codes are
- (IBAction)ButtonTouched:(id)sender {
if(!UIAccessibilityIsReduceTransparencyEnabled()){
self.view.backgroundColor = [UIColor clearColor];
UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *blureffectview = [[UIVisualEffectView alloc]initWithEffect:blurEffect];
blureffectview.frame = self.view.bounds;
blureffectview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
//[self.view addSubview:blureffectview];
[self.view insertSubview:_AnimatedViewForLoading aboveSubview:blureffectview];
}else{
//self.view.backgroundColor = [UIColor blackColor];
}
UIView * AnimatedViewForLoading;
// [[UIView alloc] initWithFrame:CGRectMake(118, 318, 200, 150)];
self.AnimatedViewForLoading.frame = CGRectMake(118, 318, 200, 150);
[self.AnimatedViewForLoading setBackgroundColor:[UIColor blueColor]];
self.AnimatedViewForLoading.alpha = 0.0f;
//AnimatedViewForLoading.backgroundColor = UIColor.lightGrayColor;
self.AnimatedViewForLoading.transform = CGAffineTransformMakeScale(0.01, 0.01);
[self.view addSubview:self.AnimatedViewForLoading];
[UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.AnimatedViewForLoading.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
} completion:^(BOOL finished) {
NSLog(#"oopssssss..........");
[UIView animateWithDuration:0.3/2 animations:^{
self.AnimatedViewForLoading.transform = CGAffineTransformIdentity;
}];
}];
}
Problem
Why doesn't blur background appear? - Because you didn't add it to self.view
//[self.view addSubview:blureffectview];
Why doesn't AnimatedViewForLoading appear? - Because you added it to self.view before it's initialized and self.AnimatedViewForLoading.alpha is set to 0.0f
// self.AnimatedViewForLoading is hidden at this line
self.AnimatedViewForLoading.alpha = 0.0f;
// |_AnimatedViewForLoading| is initialized after this line
[self.view insertSubview:_AnimatedViewForLoading aboveSubview:blureffectview];
Solution
- (IBAction)ButtonTouched:(id)sender {
// UIView * AnimatedViewForLoading;
// [[UIView alloc] initWithFrame:CGRectMake(118, 318, 200, 150)];
self.AnimatedViewForLoading = [[UIView alloc] initWithFrame:CGRectMake(118, 318, 200, 150)];
[self.AnimatedViewForLoading setBackgroundColor:[UIColor blueColor]];
// self.AnimatedViewForLoading.alpha = 0.0f;
//AnimatedViewForLoading.backgroundColor = UIColor.lightGrayColor;
// if(!UIAccessibilityIsReduceTransparencyEnabled()){
// self.view.backgroundColor = [UIColor clearColor];
UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *blureffectview = [[UIVisualEffectView alloc]initWithEffect:blurEffect];
blureffectview.frame = self.view.bounds;
blureffectview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:blureffectview];
[self.view insertSubview:_AnimatedViewForLoading aboveSubview:blureffectview];
// }else{
//self.view.backgroundColor = [UIColor blackColor];
// }
self.AnimatedViewForLoading.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.AnimatedViewForLoading.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
} completion:^(BOOL finished) {
NSLog(#"oopssssss..........");
[UIView animateWithDuration:0.3/2 animations:^{
self.AnimatedViewForLoading.transform = CGAffineTransformIdentity;
}];
}];
}
I have added a UIView and now, i want to show a UILabel. However, it does not get displayed. Can someone help me out please ?
- (IBAction)infoButtonClicked:(id)sender {
myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
myView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(5, 10, self.view.frame.size.width, 100)];
[myView addSubview:label];
[self.view addSubview: myView];
[UIView animateWithDuration:0.3/1.5 animations:^{
myView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.3/2 animations:^{
myView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.3/2 animations:^{
myView.transform = CGAffineTransformIdentity;
label.text=#"The title";
}];
}];
}];
//The setup code to detect single touch
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(oneTap:)];
[myView addGestureRecognizer:singleFingerTap];
}
try this
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(5, 10, myView.frame.size.width, 100)];
label.text = #"sample TExt";
label.numberOfLines = 1;
label.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; // or UIBaselineAdjustmentAlignCenters, or UIBaselineAdjustmentNone
label.adjustsFontSizeToFitWidth = YES;
label.adjustsLetterSpacingToFitWidth = YES;
label.minimumScaleFactor = 10.0f/12.0f;
label.clipsToBounds = YES;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.textAlignment = NSTextAlignmentLeft;
[myView addSubview:label];
I'm setting up a subclass of UIView to roll my own UIAlertView style view. I've got everything set up properly with displaying the view, but I'm at a bit of a loss as to how to dismiss the view properly. Specifically, when a user taps on the button in the view, it needs to animate out of the main view. This is the code for the view itself:
+ (void)showCustomAlertWithTitle:(NSString *)titleString andMessage:(NSString *)messageString inView:(UIView *)view andButton1Title: (NSString *)button1Title andButton2Title: (NSString *)button2Title
{
UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];
CGRect windowFrame = window.frame;
[view setAlpha:0.5f];
UIColor *buttonColor = [UIColor colorWithRed:0/255.0f green:130/255.0f blue:216/255.0f alpha:1];
UIColor *titleColor = [UIColor colorWithRed:0/255.0f green:153/255.0f blue:102/255.0f alpha:1];
// Shade
UILabel *shadeWindow = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, windowFrame.size.width, windowFrame.size.height)];
shadeWindow.backgroundColor = [UIColor blackColor];
shadeWindow.alpha = 0.50f;
// Define size and origin of alert box
float alertBoxHeight = 225;
float alertBoxWidth = 200;
float alertBoxXorigin = windowFrame.size.width / 2 - (alertBoxWidth / 2);
float alertBoxYorigin = windowFrame.size.height / 2 - (alertBoxHeight / 2);
// Initialize background
UIView *alertBackground = [[UIView alloc] initWithFrame:CGRectMake(alertBoxXorigin, alertBoxYorigin, alertBoxWidth, alertBoxHeight)];
alertBackground.layer.cornerRadius = 5.0f;
[alertBackground.layer setMasksToBounds:YES];
alertBackground.layer.backgroundColor = [UIColor whiteColor].CGColor;
// Title Label
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, alertBoxWidth, 40)];
titleLabel.text = titleString;
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.textColor = titleColor;
titleLabel.layer.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.05f].CGColor;
[titleLabel setFont:[UIFont fontWithName:#"AvenirNext-Bold" size:20.0]];
// Title Divider
UILabel *titleDivider = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, alertBoxWidth, 1.0)];
titleDivider.backgroundColor = [UIColor grayColor];
titleDivider.alpha = 0.5f;
// Alert Message Text
UITextView *alertMessage = [[UITextView alloc] initWithFrame:CGRectMake(0, 40, alertBoxWidth, alertBoxHeight - 90)];
alertMessage.text = messageString;
alertMessage.layer.backgroundColor = [UIColor whiteColor].CGColor;
[alertMessage setFont:[UIFont fontWithName:#"Avenir" size:15.0]];
alertMessage.textAlignment = NSTextAlignmentCenter;
alertMessage.textColor = [UIColor grayColor];
[alertMessage setEditable:NO];
// Button 1
UIButton *button1 = [[UIButton alloc] init];
UIButton *button2 = [[UIButton alloc] init];
[button1 addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
if (button2Title == nil)
{
button1.frame = CGRectMake(10, alertBoxHeight - 50, alertBoxWidth - 20, 40);
}
else
{
button1.frame = CGRectMake(10, alertBoxHeight - 50, (alertBoxWidth / 2) - 20, 40);
button2.frame = CGRectMake(alertBoxWidth / 2 + 10, alertBoxHeight - 50, (alertBoxWidth / 2) - 20, 40);
}
button1.layer.backgroundColor = buttonColor.CGColor;
[button1 setTitle:button1Title forState:UIControlStateNormal];
[button1.titleLabel setFont:[UIFont fontWithName:#"AvenirNext-Bold" size:15.0f]];
button1.layer.cornerRadius = 2.5f;
[button1.layer setMasksToBounds:YES];
// Button 2
button2.layer.backgroundColor = buttonColor.CGColor;
[button2 setTitle:button2Title forState:UIControlStateNormal];
[button2.titleLabel setFont:[UIFont fontWithName:#"AvenirNext-Bold" size:15.0f]];
button2.layer.cornerRadius = 2.5f;
[button2.layer setMasksToBounds:YES];
// Bounce Implementation
alertBackground.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^
{
alertBackground.transform = CGAffineTransformIdentity;
}
completion:^(BOOL finished)
{
// do something once the animation finishes, put it here
}];
[window addSubview:view];
[window addSubview:alertBackground];
[view addSubview:shadeWindow];
[view bringSubviewToFront:alertBackground];
[alertBackground addSubview:button1];
[alertBackground addSubview:titleLabel];
[alertBackground addSubview:titleDivider];
[alertBackground addSubview:alertMessage];
[alertBackground addSubview:button2];
[alertBackground bringSubviewToFront:titleDivider];
/* [[[customAlerts sharedInstance] subViewArray] addObject:alertBackground];
[[[customAlerts sharedInstance] subViewArray] addObject:view];
[[[customAlerts sharedInstance] subViewArray] addObject:window];*/
}
When button1 is tapped, for instance, I need to have it animate the view out of the superView and remove it from the stack. I'm not sure how to handle this. Does anyone have any ideas?
To permanently remove a view, I generally use the UIView instance method removeFromSuperview. followed by a line setting the relevant variable to nil:
[myAlertView removeFromSuperview];
myAlertView = nil;
In your case then, I think you need to move the view off the screen by animating the its bounds property, then use the above couple of lines to remove any references to it.
Just found the answer here:
Dismiss view controller from #selector without creating seperate method
Had to download some custom classes but it worked:
[button1 addEventHandler:^(id sender, UIEvent *event)
{
[UIView animateWithDuration:0.25f animations:^{
[alertBackground setAlpha:0.0f];
[shadeWindow setAlpha:0.0f];
[window setAlpha:0.0f];
}];
} forControlEvent:UIControlEventTouchUpInside];
Custom classes can be found here:
https://github.com/ZeR0-Wu/JTTargetActionBlock
I've looked all over Google & Stack Overflow, but I haven't found one clear answer. How do I switch to the TOCViewController after tapping the UIButton? I'm currently using "presentViewController" but it just freezes on tapping the button. Thanks in advance for any help.
#import "SplashLoginViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "TOCViewController.h"
#interface SplashLoginViewController ()
#end
#implementation SplashLoginViewController
- (void) displayTOC {
TOCViewController *toc = [[TOCViewController alloc] init];
[self presentViewController:toc animated:YES completion:nil];
}
- (void)initializeSplashView
{
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.view.bounds.size.height)];
backgroundView.backgroundColor = [UIColor colorWithRed:52.0/255.0 green:170.0/255.0 blue:220.0/255.0 alpha:1.0];
backgroundView.tag = 1;
backgroundView.layer.zPosition = 99.0f;
[self.view addSubview:backgroundView];
UILabel *codeworksLabelView = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width, self.view.bounds.size.height / 2.0 - 25.0, self.view.bounds.size.width, 50.0)];
codeworksLabelView.text = #"Codeworks";
codeworksLabelView.textAlignment = NSTextAlignmentCenter;
codeworksLabelView.textColor = [UIColor whiteColor];
codeworksLabelView.font = [UIFont fontWithName:#"Montserrat" size:30.0];
codeworksLabelView.tag = 2;
codeworksLabelView.layer.zPosition = 100.0f;
[self.view addSubview:codeworksLabelView];
}
- (void)initializeLoginView
{
CGRect loginFrame = CGRectMake(50.0, 100.0, self.view.bounds.size.width - 100.0, 30.0);
CGRect passwordFrame = CGRectMake(50.0, 180.0, self.view.bounds.size.width - 100.0, 30.0);
UITextField *usernameInput = [[UITextField alloc] init];
usernameInput.frame = loginFrame;
usernameInput.placeholder = #"Username";
usernameInput.font = [UIFont fontWithName:#"Montserrat" size:15.0];
usernameInput.returnKeyType = UIReturnKeyNext;
usernameInput.tag = 3;
usernameInput.autocorrectionType = UITextAutocorrectionTypeNo;
UITextField *passwordInput = [[UITextField alloc] init];
passwordInput.frame = passwordFrame;
passwordInput.placeholder = #"Password";
passwordInput.font = [UIFont fontWithName:#"Montserrat" size:15.0];
passwordInput.returnKeyType = UIReturnKeyDone;
passwordInput.tag = 4;
passwordInput.secureTextEntry = YES;
CALayer *bottomBorder = [CALayer layer];
bottomBorder.frame = CGRectMake(0.0, 29.0, usernameInput.frame.size.width, 1.0);
bottomBorder.backgroundColor = [UIColor blackColor].CGColor;
CALayer *borderCopy = [CALayer layer];
borderCopy.frame = CGRectMake(0.0, 29.0, passwordInput.frame.size.width, 1.0);
borderCopy.backgroundColor = [UIColor blackColor].CGColor;
[usernameInput.layer addSublayer:bottomBorder];
[passwordInput.layer addSublayer:borderCopy];
[self.view addSubview:usernameInput];
[self.view addSubview:passwordInput];
UILabel *signInButton = [[UILabel alloc] initWithFrame:CGRectMake(0, 270, self.view.bounds.size.width, 60.0)];
signInButton.font = [UIFont fontWithName:#"Montserrat" size:15.0];
signInButton.text = #"Sign In";
signInButton.textColor = [UIColor blackColor];
signInButton.tag = 5;
signInButton.textAlignment = NSTextAlignmentCenter;
CALayer *topButtonBorder = [CALayer layer];
topButtonBorder.frame = CGRectMake(0, 0, self.view.bounds.size.width, 1);
topButtonBorder.backgroundColor = [UIColor blackColor].CGColor;
CALayer *bottomButtonBorder = [CALayer layer];
bottomButtonBorder.frame = CGRectMake(0, 59, self.view.bounds.size.width, 1);
bottomButtonBorder.backgroundColor = [UIColor blackColor].CGColor;
[signInButton.layer addSublayer:topButtonBorder];
[signInButton.layer addSublayer:bottomButtonBorder];
[self.view addSubview:signInButton];
UIView *optionsDivide = [[UIView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 2 - 0.5, self.view.bounds.size.height - 45, 1, 20)];
optionsDivide.backgroundColor = [UIColor blackColor];
[self.view addSubview:optionsDivide];
UIButton *tos = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *pp = [UIButton buttonWithType:UIButtonTypeRoundedRect];
tos.frame = CGRectMake(self.view.bounds.size.width / 2 - 115.5 , self.view.bounds.size.height - 65, 110, 60);
pp.frame = CGRectMake(self.view.bounds.size.width / 2 + 0.5 , self.view.bounds.size.height - 65, 110, 60);
[tos setTitle:#"Terms of Service" forState:UIControlStateNormal];
[pp setTitle:#"Privacy Policy" forState:UIControlStateNormal];
pp.titleLabel.textAlignment = NSTextAlignmentLeft;
tos.titleLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:12.0];
pp.titleLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:12.0];
[tos addTarget:self action:#selector(displayTOC:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:tos];
[self.view addSubview:pp];
}
- (void)animateSplashScreen
{
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self.view viewWithTag:2].frame = CGRectMake(0.0, self.view.bounds.size.height / 2.0 - 25.0, self.view.bounds.size.width, 50.0);
}
completion:^(BOOL finished){
[self initializeLoginView];
[UIView animateWithDuration:0.5
delay:1.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
[self.view viewWithTag:1].frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, 65.0);
[self.view viewWithTag:2].frame = CGRectMake(0.0, 15.0, self.view.bounds.size.width, 50.0);
((UILabel *)[self.view viewWithTag:2]).transform = CGAffineTransformScale(((UILabel *)[self.view viewWithTag:2]).transform, 0.6, 0.6);
}
completion:^(BOOL finished){
((UILabel *)[self.view viewWithTag:2]).font = [UIFont fontWithName:#"Montserrat" size:30.0];
}];
}];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initializeSplashView];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self animateSplashScreen];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//CGPoint locationPoint = [[touches anyObject] locationInView:self.view];
//CGPoint viewPoint = [[self.view viewWithTag:5] convertPoint:locationPoint fromView:self.view];
//if ([[self.view viewWithTag:5] pointInside:viewPoint withEvent:event]) {
// [self.view viewWithTag:5].frame = CGRectMake(self.view.bounds.size.width / 2 - 62.5, 254.5, 130.0, 60.0);
//}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//[self.view viewWithTag:5].frame = CGRectMake(self.view.bounds.size.width / 2 - 67.5, 249.5, 130.0, 60.0);
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
//[self.view viewWithTag:5].frame = CGRectMake(self.view.bounds.size.width / 2 - 67.5, 249.5, 130.0, 60.0);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#end
I copy-pasted your code into a file and found a warning on this line [tos addTarget:self action:#selector(displayTOC:) forControlEvents:UIControlEventTouchUpInside]; The warning says,
Undeclared selector 'displayTOC:'
When I ran the code and tapped the TOC button, it didn't just freeze, it crashed with the error:
-[ViewController displayTOC:]: unrecognized selector sent to instance ...
These should be strong hints on what is wrong in your code. When you ask people about your code, you should reveal any and all warnings, and crashes.
Note, a method named displayTOC is not the same as a method named displayTOC:
By the way, the animation you did is really cool. Good job!
I have an image view as a subview and I'm trying to implement a swipe up gesture to fling it off screen. The swipe works but it doesn't animate.
This is how I'm adding the subview:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
InstagramMedia *media = mediaArray[indexPath.row];
for (int i = 0; i < mediaArray.count; i++)
{
InstagramMedia *instagramMedia = [mediaArray objectAtIndex:i];
[imageArray addObject:instagramMedia.standardResolutionImageURL];
}
largeImageArray = imageArray;
imageIndex = indexPath.row;
UICollectionViewLayoutAttributes *attributes = [self.collectionView layoutAttributesForItemAtIndexPath:indexPath];
CGRect cellRect = attributes.frame;
blurredView = [[UIToolbar alloc]initWithFrame:self.view.bounds];
[blurredView setBarStyle:UIBarStyleBlackOpaque];
[self.view addSubview:blurredView];
imageView = [[UIImageView alloc]initWithFrame:cellRect];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.userInteractionEnabled = YES;
[imageView setImageWithURL:media.standardResolutionImageURL placeholderImage:[UIImage imageNamed:#"placeholder"]];
[self.view addSubview:imageView];
CGRect finalFrame = CGRectMake(0, 50, 320, 301);
[UIView animateWithDuration:0.5 animations:^{
imageView.frame = finalFrame;
}];
UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swipeLeft:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeRecognizer];
UISwipeGestureRecognizer *swipeRightRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swipeRight:)];
swipeRightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRightRecognizer];
swipeUpRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swipeUp:)];
swipeUpRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeUpRecognizer];
}
And this is what I'm trying to do:
- (IBAction)swipeUp:(id)sender
{
CGRect finalFrame = CGRectMake(0, -100, 320, 301);
[UIView animateWithDuration:0.5 animations:^{
imageView.frame = finalFrame;
[imageView removeFromSuperview];
[blurredView removeFromSuperview];
[self.view removeGestureRecognizer:swipeUpRecognizer];
}];
}
- (IBAction)swipeUp:(id)sender
{
CGRect finalFrame = CGRectMake(0, -100, 320, 301);
[UIView animateWithDuration:0.5 animations:^{
imageView.frame = finalFrame;
} completion:^(BOOL finished) {
[imageView removeFromSuperview];
[blurredView removeFromSuperview];
[self.view removeGestureRecognizer:swipeUpRecognizer];
}];
}
You were removing view before the animation ends... that's why its' happening.