Create A Shadow(or blur) when user Click on UIbutton? - ios

I have a One Uibutton in Buttom. when User click on that button a uiview animation is display like popup from buttom to up.
this is my code
-(void)viewDidLoad
{
[super viewDidLoad];
self.BuyButtonPopUpView.frame = CGRectMake(self.BuyButtonPopUpView.frame.origin.x, 1000, self.BuyButtonPopUpView.frame.size.width,self.BuyButtonPopUpView.frame.size.height);
}
- (IBAction)animated:(id)sender
{
if(_isAnimated)
{
[UIView animateWithDuration:0.3
delay:0.0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
self.BuyButtonPopUpView.frame = CGRectMake(self.BuyButtonPopUpView.frame.origin.x, 520, self.BuyButtonPopUpView.frame.size.width,self.BuyButtonPopUpView.frame.size.height);
}
completion:^(BOOL finished){
}];
_isAnimated=NO;
}
else
{
[UIView animateWithDuration:0.3
delay:0.0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
self.BuyButtonPopUpView.frame = CGRectMake(self.BuyButtonPopUpView.frame.origin.x, 1000, self.BuyButtonPopUpView.frame.size.width,self.BuyButtonPopUpView.frame.size.height);
}
completion:^(BOOL finished)
{
}];
_isAnimated=YES;
}
}
Uiview animation is perfectly working but when uiview appear than background view is create shadow(or blur)like uiactionsheet and after finished animation background view is clear.
i dont know how to implemented.
plz help me
i am new in ios...

You can add a UIView in background and set its backgroundcolor black and alpha 0.5 in storyboard. Then change its alpha from 0.5 to 0 with animation. You can also add an UITapGestureRecognizer to detect taps on it and dissmiss the view when user taps outside.
-(void)viewDidLoad
{
[super viewDidLoad];
self.BuyButtonPopUpView.frame = CGRectMake(self.BuyButtonPopUpView.frame.origin.x, 1000, self.BuyButtonPopUpView.frame.size.width,self.BuyButtonPopUpView.frame.size.height);
}
- (IBAction)animated:(id)sender
{
if(_isAnimated)
{
[UIView animateWithDuration:0.3
delay:0.0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
self.BuyButtonPopUpView.frame = CGRectMake(self.BuyButtonPopUpView.frame.origin.x, 520, self.BuyButtonPopUpView.frame.size.width,self.BuyButtonPopUpView.frame.size.height);
//showing background view here
self.viewBackground.alpha = 0.5;
}
completion:^(BOOL finished){
}];
_isAnimated=NO;
}
else
{
[UIView animateWithDuration:0.3
delay:0.0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
self.BuyButtonPopUpView.frame = CGRectMake(self.BuyButtonPopUpView.frame.origin.x, 1000, self.BuyButtonPopUpView.frame.size.width,self.BuyButtonPopUpView.frame.size.height);
//dismissing background view here
self.viewBackground.alpha = 0.0;
}
completion:^(BOOL finished)
{
}];
_isAnimated=YES;
}
}
For detecting taps outside and dismissing your view:
Create a UITapGestureRecognizer:
#interface ViewController ()
{
UITapGestureRecognizer *tapGesture;
}
Initialize it in ViewDidLoad:
-(void)viewDidLoad
{
//defining your gesture method
tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(handleSingleTapGesture:)];
//adding gesture recognizer on the background view
[self.backgroundView addGestureRecognizer:tapGestureRecognizer];
}
In your Gesture Recognizer Method:
-(void)handleSingleTapGesture:(UITapGestureRecognizer *)tapGestureRecognizer
{
[UIView animateWithDuration:0.3
animations:^{
//dismissing background view here
self.viewBackground.alpha = 0.0;
}
completion:^(BOOL finished){
}];
}
Hope this helps.

add your code in viewdidload.add QuartzCore framework
#import <QuartzCore/CAAnimation.h>
self.BuyButtonPopUpView.frame = CGRectMake(self.BuyButtonPopUpView.frame.origin.x, 1000, self.BuyButtonPopUpView.frame.size.width,self.BuyButtonPopUpView.frame.size.height);
CABasicAnimation *animate = [CABasicAnimation animationWithKeyPath:#"shadowOpacity"];
animate.fromValue = [NSNumber numberWithFloat:1.5];
animate.toValue = [NSNumber numberWithFloat:0.5];
animate.duration = 1.0;
[self.BuyButtonPopUpView.layer addAnimation:animation forKey:#"shadowOpacity"];
self.BuyButtonPopUpView.layer.shadowOpacity = 0.0;

you can add a UIVisualEffectView first then add the popup over this view.
here is a tutorial link https://www.raywenderlich.com/84043/ios-8-visual-effects-tutorial

Related

Continous fade in and fade out

Im trying to fade in and out of a light node I have created I just dont know where to call the function accordingly so it will continously fade in and out. Never tried animating anything here is what I have.
- (void)fadeOutIn:(UIView *)view duration:(NSTimeInterval)duration
{
[self enumerateChildNodesWithName:#"//*" usingBlock:^(SKNode *node, BOOL *stop) {
if ([node.name isEqualToString:#"light1"]) {
[UIView animateKeyframesWithDuration:5 delay:2 options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse|UIViewAnimationCurveEaseInOut animations:^{
node.alpha = 0;
} completion:^(BOOL finished) {
}];
}
}];
}
where should I call this function?that is fades in and out continously?
Should it be called within an action?
Thank You
Use this for Continue fade in out.
-(void)viewDidAppear:(BOOL)animated{
[UIView animateKeyframesWithDuration:1 delay:0 options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse|UIViewAnimationCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction animations:^{
node.alpha = 0;
} completion:^(BOOL finished) {
[UIView animateWithDuration:1 animations:^{
node.alpha = 1;
} completion:nil];
}];
}
-(void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[objAnimate setAlpha:1.0];
[UIView animateKeyframesWithDuration:2.0 delay:0.0 options:UIViewKeyframeAnimationOptionCalculationModeLinear | UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse animations:^{
[objAnimate setAlpha:0.0];
} completion:nil];
}
This will create continuous fade in and out for your object when it appears. If you want to create programmatically a custom object and then use this code then do it after your custom object is added in your view as below (example):
objAnimate = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, 200, 40)];
objAnimate.backgroundColor = [UIColor lightGrayColor];
[objAnimate setAlpha:1.0];
[self.view addSubview:objAnimate];
[UIView animateKeyframesWithDuration:2.0 delay:0.0 options:UIViewKeyframeAnimationOptionCalculationModeLinear | UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse animations:^{
[objAnimate setAlpha:0.0];
} completion:nil];
The view starts animating as soon as its added in your view.

UIPickerView Animate on and off screen behavior

This is a continuation of this question
CGRectMake : How To Calculate X and Y For UIPickerView Animation?
answered by https://stackoverflow.com/users/2315974/danypata
I have a picker view that I want to animate on and off screen on a button press and using the code in the previous question/answer I have got it to animate on screen the first time the button is pressed.
the second tome the button is pressed it just disappears and then the third time it is pressed it animates to the wrong place...please help
the code
if (self.pickerSort.hidden == NO) {
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
self.pickerSort.frame = CGRectMake(0,self.view.frame.size.height
+ self.pickerSort.frame.size.height,-self.pickerSort.frame.size.width,-self.pickerSort.frame.size.height);
}
completion:^(BOOL finished) {
}];
self.pickerSort.hidden = YES;
// [self performSelector:#selector(hidePicker) withObject:nil afterDelay:1];
} else if (self.pickerSort.hidden == YES) {
self.pickerSort.hidden = NO;
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
self.pickerSort.frame = CGRectMake(0 ,
self.view.frame.size.height
- self.pickerSort.frame.size.height,
self.pickerSort.frame.size.width,
self.pickerSort.frame.size.height);
}
completion:^(BOOL finished) {
}];
[self.view bringSubviewToFront:self.pickerSort];
Behavior in images - button press animates onto screen beautifully
Second Press it disappears with no animation
Third Press it animates to here
Any help would be great...functionality I am looking for is how to put the picker view back on an animation so the 3rd press is the same as the first
Please try to use the below code.
self.pickerSort.hidden = NO;
NSTimeInterval duration = 0.5;
[UIView animateWithDuration:duration
delay:0
options:UIViewAnimationOptionCurveLinear
animations:^{
CGRect pickerFrame = self.pickerSort.frame;
// currently picker is off the screen, show it.
if (pickerFrame.origin.y == self.view.frame.size.height) {
// set frame to show picker
pickerFrame.origin.y = self.view.frame.size.height - self.pickerSort.frame.size.height;
} else {
// set frame to hide picker
pickerFrame.origin.y = self.view.frame.size.height;
}
self.pickerSort.frame = pickerFrame;
}
completion:^(BOOL finished) {
self.pickerSort.hidden = YES;
}];
After playing around with this and learning the ins and outs of frames and CGRects, I achieved this by using the following code
if (pickerSort.hidden == YES) {
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
pickerSort.hidden = NO;
visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
[self.tableStations addSubview:visualEffectView];
pickerSort.frame = CGRectMake(0,0,originalPickerFrame.size.width,originalPickerFrame.size.height);
visualEffectView.frame = CGRectMake(0,0,originalPickerFrame.size.width,originalPickerFrame.size.height - 64);
}
completion:^(BOOL finished) {
self.navigationController.navigationItem.leftBarButtonItem.enabled = NO;
}];
}
else
{
visualEffectView.hidden = YES;
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
pickerSort.frame = CGRectMake(0,-originalPickerFrame.size.height,0,0);
}
completion:^(BOOL finished) {
self.navigationController.navigationItem.leftBarButtonItem.enabled = YES;
pickerSort.hidden = YES;
}];
}
I set the original frame size in viewDidLoad like so
pickerSort = [[UIPickerView alloc]initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];
originalPickerFrame = pickerSort.frame;
pickerSort.frame = CGRectMake(0,-originalPickerFrame.size.height,0, 0);
Hope this can help somebody in the future

iOS Views return to original locations when textView becomesFirstResponder

Hello and thank you in advance...
I am moving views frame location in animation blocks. After I move views, I am tapping a textView to allow for editing/input. When the textView is tapped, the views I moved previously pop back to their original (unmoved) frame locations.
I'm not sure what I am doing incorrectly here.
THIS IS HOW I SETUP THE DIFFERENT FRAME POSITIONS TO ANIMATE TO
//Books active and rest position will effect other elements (paper and paperTextView)
self.bookRestPosition = CGRectMake((self.view.frame.size.width * 0.10), (self.view.frame.size.height * 0.4), self.view.frame.size.width, self.view.frame.size.height);
self.bookActivePosition = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y +100, self.view.frame.size.width, self.view.frame.size.height);
self.bookOpenPosition = CGRectMake(self.bookActivePosition.origin.x, self.bookActivePosition.origin.y, 20, self.bookActivePosition.size.height);
self.quillRestPosition = self.quill.frame;
self.quillActivePosition = CGRectMake(self.quill.frame.origin.x, self.view.frame.origin.y -200, self.quill.frame.size.width, self.quill.frame.size.height);
self.woodTableRestPosition = self.view.frame;
self.woodTableActivePosition = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y -200, self.view.frame.size.width, self.view.frame.size.height +200);
self.paperRestPosition = self.bookRestPosition;
self.paperActivePosition = CGRectMake(self.bookOpenPosition.size.width, self.bookActivePosition.origin.y, self.bookActivePosition.size.width, self.bookActivePosition.size.height);
self.paperTextViewRestPosition = self.bookRestPosition;
self.paperTextViewActivePosition = self.paperActivePosition;
self.paperTextView.editable = NO;
self.paper.alpha = 0.0;
self.paperTextView.alpha = 0.0;
self.paper.frame = self.bookRestPosition;
self.paperTextView.frame = self.bookRestPosition;
THIS IS HOW I ANIMATE THEM
- (IBAction)onBookTapped:(UITapGestureRecognizer *)sender {
if (self.logCover.frame.origin.x == self.bookRestPosition.origin.x) {
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self activateLogAndViewsAssociated];
} completion:^(BOOL finished) {
NSLog(#"Book Slid up!");
}];
}
else if (self.logCover.frame.origin.x == 0){
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self everythingAtRestPosition];
} completion:^(BOOL finished) {
NSLog(#"Book Slid Down!");
}];
}
}
- (IBAction)swipeBookLeft:(UISwipeGestureRecognizer *)sender {
//if (self.logCoverImage.frame.origin.x == 0) {
self.paper.alpha = 1.0;
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self.logCover setFrame:self.bookOpenPosition];
self.editButton.alpha = 1.0;
self.editButton.enabled = YES;
} completion:^(BOOL finished) {
NSLog(#"Book Has Opened!");
//This must be here. The paperTextView is what the swipe right gesture is attached to
self.paperTextView.alpha = 1.0;
// self.logCoverImage.alpha = 0.0;
// self.quill.alpha = 0.0;
}];
//}
}
- (IBAction)onPaperSwipeRight:(UISwipeGestureRecognizer *)sender {
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self.logCover setFrame:self.bookActivePosition];
self.editButton.alpha = 0.0;
self.editButton.enabled = NO;
} completion:^(BOOL finished) {
NSLog(#"Book Has Closed!");
//self.paper.alpha = 0.0;
// [NSUserDefaults *log = [NSUserDefaults standardUserDefaults];
// [log setObject:self.paperTextView forKey:#"textLog"];
}];
}
- (IBAction)onBookSwipeDown:(UISwipeGestureRecognizer *)sender {
if (self.logCover.frame.origin.x == 0) {
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self everythingAtRestPosition];
} completion:^(BOOL finished) {
NSLog(#"Book Slid Down!");
}];
}
}
- (IBAction)onBookSwipeUp:(UISwipeGestureRecognizer *)sender {
if (self.logCover.frame.origin.x == self.bookRestPosition.origin.x) {
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self activateLogAndViewsAssociated];
} completion:^(BOOL finished) {
NSLog(#"Book Slid up!");
}];
}
}
- (void)animateAtStartup
{
self.logCover.frame = CGRectMake(self.view.frame.size.width, self.view.frame.size.height, self.bookRestPosition.size.width, self.bookRestPosition.size.height);
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self.logCover setFrame:self.bookRestPosition];
} completion:^(BOOL finished) {
NSLog(#"Book Slid Down!");
}];
}
AND THIS IS WHAT HAPPENS WHEN THE EDIT BUTTON IS PRESSED
- (IBAction)onEditButtonPressed:(id)sender
{
self.paperTextView.editable = YES;
[self.paperTextView becomeFirstResponder];
}

CGAffineTransformMakeRotation again won't work

I create a UIView and set it's layer's contents a image. And I added a tap gesture to this view. When I tap the view, I want it to rotate 180 degree, and when I click close button, I want it to rotate to it's original degree. But it won't work when I try to rotate it back.
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:#"menuArray"];
self.menuView.layer.contents = (__bridge id)image.CGImage;
self.menuView.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap)];
[self.menuView addGestureRecognizer:tap];
}
//rotate it 180 degree
- (void)tap
{
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseIn animations:^{
self.menuView.layer.affineTransform = CGAffineTransformMakeRotation(M_PI);
} completion:^(BOOL finished) {
}];
}
//rotate it back. this won't work. I have change the value from M_PI to M_PI_2, M_PI_4. it's not working.
- (IBAction)close:(id)sender
{
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseIn animations:^{
self.menuView.layer.affineTransform = CGAffineTransformMakeRotation(-M_PI);
} completion:^(BOOL finished) {
}];
}
To undo an affineTransorm you should set it to the constant affineTransformIdentity , which is tranform of nil so to speak, a matrix of all zeros..
So in your second method where you want to rotate back set self.menuView.layer.affineTransform = CGAffineTransformIdentity

iOS Button not clickable during animation

When I added animation to my button using a category method, then I can't click that button, seems it is disabled:
[_compassCalibrateButton pulse:1.5 continuously:YES];
_compassCalibrateButton.userInteractionEnabled=YES;
I have a UIView category contatining this :
- (void)pulse:(float)secs continuously:(BOOL)continuously {
[UIView animateWithDuration:secs/2
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^{
// Fade out, but not completely
self.alpha = 0.3;
}
completion:^(BOOL finished) {
[UIView animateWithDuration:secs/2
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^{
// Fade in
self.alpha = 1.0;
}
completion:^(BOOL finished) {
if (continuously) {
[self pulse:secs continuously:continuously];
}
}];
}];
}
From the doc
During an animation, user interactions are temporarily disabled for
the views being animated. (Prior to iOS 5, user interactions are
disabled for the entire application.) If you want users to be able to
interact with the views, include the
UIViewAnimationOptionAllowUserInteraction constant in the options
parameter.
So your code should be
- (void)pulse:(float)secs continuously:(BOOL)continuously {
[UIView animateWithDuration:secs/2
delay:0.0
options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
animations:^{
// Fade out, but not completely
self.alpha = 0.3;
}
completion:^(BOOL finished) {
[UIView animateWithDuration:secs/2
delay:0.0
options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
animations:^{
// Fade in
self.alpha = 1.0;
}
completion:^(BOOL finished) {
if (continuously) {
[self pulse:secs continuously:continuously];
}
}];
}];
}
I tested on Swift 4
UIView.animate(withDuration: 0.8, delay: 0, options: [.repeat, .autoreverse, .allowUserInteraction], animations: {
// Do Something
}, completion: nil)
Try this it will help you
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
for (UIButton *button in self.arrBtn)
{
if ([button.layer.presentationLayer hitTest:touchLocation])
{
// This button was hit whilst moving - do something with it here
[button setBackgroundColor:[UIColor cyanColor]];
NSLog(#"Button Clicked");
break;
}
}
}
Note that if you are animating a UIButton to fade out after a while, e.g.:
[UIView animateWithDuration:1.0 delay:3.0 options:|UIViewAnimationOptionAllowUserInteraction animations:^{
self->btn.alpha = 0;
} completion:nil];
It will be unresponsive from the very start, regardless your 'delay', due to the final alpha. One way around this is something like:
[UIView animateWithDuration:1.0 delay:3.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
self->btn.alpha = 0.1;
} completion:^(BOOL finished){self->btn.alpha = 0;}];

Resources