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;
}];
}];
}
Related
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've done this a million times before but finding myself confused now.
When you animate a container view to change its position, its subviews move along with it don't they? You don't change subviews' frames you just change superview's frame because all subviews' positions inside their superview don't change.
But for some reason this time the subviews would not move along with their superview, it'd stay stuck at its original position.
- (void)writeCommentTapped{
UIView *writeCommentView = [[UIView alloc]init];
writeCommentView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview: writeCommentView];
[self.view bringSubviewToFront:self.navView];
self.writeCommentTextView = [[UITextView alloc]init];
self.writeCommentTextView.backgroundColor = [UIColor blackColor];
[writeCommentView addSubview:self.writeCommentTextView];
[self.writeCommentTextView becomeFirstResponder];
self.writeCommentTextView.returnKeyType = UIReturnKeyDefault;
writeCommentView.frame = CGRectMake(0, -self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - self.keyboardSize.height);
self.writeCommentTextView.frame = CGRectMake(0, 0, writeCommentView.frame.size.width - 40, writeCommentView.frame.size.height - 100);
self.writeCommentTextView.center = writeCommentView.center;
[UIView animateWithDuration:0.15
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
writeCommentView.frame = CGRectMake(0, 0, writeCommentView.frame.size.width, writeCommentView.frame.size.height);
} completion:^(BOOL finished){
if (finished) {
}
}
];
}
Remove self.writeCommentTextView.center and set calculated frame for center
-(void)writeCommentTapped{
UIView *writeCommentView = [[UIView alloc]init];
writeCommentView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview: writeCommentView];
[self.view bringSubviewToFront:self.navView];
self.writeCommentTextView = [[UITextView alloc]init];
self.writeCommentTextView.backgroundColor = [UIColor blackColor];
[writeCommentView addSubview:self.writeCommentTextView];
[self.writeCommentTextView becomeFirstResponder];
self.writeCommentTextView.returnKeyType = UIReturnKeyDefault;
writeCommentView.frame = CGRectMake(0, -self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - self.keyboardSize.height);
writeCommentView.frame = CGRectMake(0, -self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height);
self.writeCommentTextView.frame = CGRectMake(40/2, 100/2, writeCommentView.frame.size.width - 40, writeCommentView.frame.size.height - 100);
// self.writeCommentTextView.center = writeCommentView.center;
[UIView animateWithDuration:0.15
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
writeCommentView.frame = CGRectMake(0, 0, writeCommentView.frame.size.width, writeCommentView.frame.size.height);
} completion:^(BOOL finished){
if (finished) {
}
}
];
}
OR
self.writeCommentTextView.center = CGPointMake(writeCommentView.frame.size.width/2.0, writeCommentView.frame.size.height/2.0);
Im trying to figure out why this isnt working
in my tableViewcontroller viewDidLoad
self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 5, 320,0)];
self.headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, 320, 0)];
self.headerLabel.textAlignment = NSTextAlignmentCenter;
self.headerLabel.text = #"text";
[self.view addSubview:self.headerView];
[self.headerView addSubview:self.headerLabel];
[UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.headerLabel.frame = CGRectMake(0, 5, 320,15);
self.headerView.frame = CGRectMake(0, 5, 320,15);
} completion:^(BOOL finished) {
[UIView animateWithDuration:.5 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.headerLabel.frame = CGRectMake(0, 5, 320,0);
self.headerView.frame = CGRectMake(0, 5, 320,0);
} completion:^(BOOL finished) {
}];
}];
if I remove the slide back up part in the completion block of the first animate call It works. The view slides down correctly. However I cannot get it
to shrink back up at all. When I include the slide up code in the completion block the view is not displayed at all on load and I dont know why and Im going insane
I'm not sure why the label disappears, but you can fix that by giving the view and label an appropriate height when you create them, and only animate the label's y position rather than its height.
- (void)viewDidLoad {
[super viewDidLoad];
self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, -30, 320,30)];
self.headerView.backgroundColor = [UIColor yellowColor];
self.headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, 320, 21)];
self.headerLabel.textAlignment = NSTextAlignmentCenter;
self.headerLabel.text = #"text";
[self.view addSubview:self.headerView];
[self.headerView addSubview:self.headerLabel];
[UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.headerView.frame = CGRectMake(0, 0, 320,30);
} completion:^(BOOL finished) {
[UIView animateWithDuration:.5 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.headerView.frame = CGRectMake(0, -30, 320,30);
} completion:^(BOOL finished) {
}];
}];
}
viewDidLoad is not really the best place to be starting animations. You should move the code to viewWillAppear: , and if you only want this to occur the first time the view appears, you should add a BOOL property to your controller (i.e. self.hasperformedInitialHeaderAnimation), so:
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
if(!self.hasPerformedInitialHeaderAnimation){
self.hasPerformedInitalHeaderAnimation = YES;
[UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.headerLabel.frame = CGRectMake(0, 5, 320,15);
self.headerView.frame = CGRectMake(0, 5, 320,15);
} completion:^(BOOL finished) {
[UIView animateWithDuration:.5 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.headerLabel.frame = CGRectMake(0, 5, 320,0);
self.headerView.frame = CGRectMake(0, 5, 320,0);
} completion:^(BOOL finished) {
}];
}
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
If you create a brand new single view app and put this code behind a button:
UIView *blah = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
blah.backgroundColor = [UIColor grayColor];
[UIView transitionWithView:blah duration:1
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
[self.view addSubview:blah];
}
completion:^(BOOL finished){
}];
The subview is added immediately with no animation. If you add the subview first then try to animate it... you get the same problem.
UIView *blah = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
[self.view addSubview:blah];
[UIView transitionWithView:blah duration:1
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
blah.backgroundColor = [UIColor grayColor];
}
completion:^(BOOL finished){
}];
How on Earth do you animate a flip for a subview while or immediately after adding it?
You generally need to have the container that constrains the animation to be in place already:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = CGRectMake(0, 0, 100, 100);
_container = [[UIView alloc] initWithFrame:frame];
_container.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:_container];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIView *subview = [[UIView alloc] initWithFrame:_container.bounds];
subview.backgroundColor = [UIColor darkGrayColor];
[UIView transitionWithView:_container
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
[_container addSubview:subview];
}
completion:NULL];
}
This is worth a try:
UIView *blah = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
blah.backgroundColor = [UIColor grayColor];
[self.view addSubview:blah];
blah.alpha = 0.0; //Or with blah.hidden = TRUE and then FALSE inside the animation block
[UIView transitionWithView:blah
duration:1
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
blah.alpha = 1.0;
}
completion:^(BOOL finished){
}];