UIView popup such as UIAlertView

I want a UIView that can display as a UIAlertView, and can also move around.

Any help please ??

Thanks.

+8
ios iphone xcode4 uiview uialertview
source share
2 answers

View transformation animation using UIView animation ( using blocks or older api )

from a small size (e.g. view.transform = CGAffineTransformMakeScale(0.1, 0.1) ) to something larger than you want (e.g. view.transform = CGAffineTransformMakeScale(1.1, 1.1)) , then go back to your desired size (view.transform = CGAffineTransformMakeScale(0.1, 0.1)) ) or add additional steps for a larger bounce.

And to move it, apply the touch methods and change the view frame when you move your finger.

Edit: here is sample code for a custom UIView similar to UIAlertView.

MyAlertView.h:

 #import <UIKit/UIKit.h> @interface MyAlertView : UIView { CGPoint lastTouchLocation; CGRect originalFrame; BOOL isShown; } @property (nonatomic) BOOL isShown; - (void)show; - (void)hide; @end 

MyAlertView.m:

 #import "MyAlertView.h" @implementation MyAlertView @synthesize isShown; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { originalFrame = frame; self.alpha = 0; self.backgroundColor = [UIColor whiteColor]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 20)]; label.text = @"Hellooooo!"; label.textAlignment = UITextAlignmentCenter; label.backgroundColor = [UIColor clearColor]; [self addSubview:label]; [label release]; UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; closeButton.frame = CGRectMake(10, frame.size.height - 45, frame.size.width - 20, 35); [closeButton setTitle:@"Close" forState:UIControlStateNormal]; [closeButton addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:closeButton]; } return self; } #pragma mark Custom alert methods - (void)show { NSLog(@"show"); isShown = YES; self.transform = CGAffineTransformMakeScale(0.1, 0.1); self.alpha = 0; [UIView beginAnimations:@"showAlert" context:nil]; [UIView setAnimationDelegate:self]; self.transform = CGAffineTransformMakeScale(1.1, 1.1); self.alpha = 1; [UIView commitAnimations]; } - (void)hide { NSLog(@"hide"); isShown = NO; [UIView beginAnimations:@"hideAlert" context:nil]; [UIView setAnimationDelegate:self]; self.transform = CGAffineTransformMakeScale(0.1, 0.1); self.alpha = 0; [UIView commitAnimations]; } - (void)toggle { if (isShown) { [self hide]; } else { [self show]; } } #pragma mark Animation delegate - (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context { if ([animationID isEqualToString:@"showAlert"]) { if (finished) { [UIView beginAnimations:nil context:nil]; self.transform = CGAffineTransformMakeScale(1.0, 1.0); [UIView commitAnimations]; } } else if ([animationID isEqualToString:@"hideAlert"]) { if (finished) { self.transform = CGAffineTransformMakeScale(1.0, 1.0); self.frame = originalFrame; } } } #pragma mark Touch methods - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; lastTouchLocation = [touch locationInView:self]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint newTouchLocation = [touch locationInView:self]; CGRect currentFrame = self.frame; CGFloat deltaX = lastTouchLocation.x - newTouchLocation.x; CGFloat deltaY = lastTouchLocation.y - newTouchLocation.y; self.frame = CGRectMake(currentFrame.origin.x - deltaX, currentFrame.origin.y - deltaY, currentFrame.size.width, currentFrame.size.height); lastTouchLocation = [touch locationInView:self]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { } @end 

Then, when you want to show this alert, you need to:

 #import "MyAlertView.h" 

and

 MyAlertView *alert = [[MyAlertView alloc] initWithFrame:CGRectMake(20, 100, 280, 100)]; [viewFromWhichYouWillShowTheAlert addSubview:alert]; [alert release]; 

then you will show it with [alert show]; , [alert hide]; with [alert hide]; or switch using [alert toggle];

You can also move it by clicking and dragging (anywhere except the close button). Hope this is enough to get you started. If you need an explanation for any part of the code, just ask.

Oh, and notice that I set the color of this view to white, so if you show it on top of another white view, you really won't see it, so just change the background color of any kind :)

+24
source share

You can get this by simply following these steps.

  • Create a 320 * 480 UIview (ViewA) to cover the entire iPhone screen with the background set to clearColor. This will serve as a super-presentation for our purpose;
  • Create another UIView (ViewB) with a size of 320 * 480 with the background set to black and opacity up to 40%. 3. Now you can add any view to the ViewB.
  • Now add ViewB to ViewA.

Finally, you can present this view where required. The effect will be, the ViewA will cover the Background viewController, the ViewB will be the server as an overwhelming effect for the background view controller, and the views in B are the UIElement that you will see.

For an animation effect, you can use some basic animation code in a UIElement in ViewB.

0
source share

All Articles