UIViewpopup像UIAlertView
我想要一个可以像UIAlertView一样popup的UIView,也可以移动。
请帮忙吗?
谢谢。
使用UIViewanimation( 使用块或更老的API )为视图的变换设置animation效果
从一些非常小的尺寸(比如view.transform = CGAffineTransformMakeScale(0.1, 0.1)
)到稍大一点的东西,然后你想要它(比如view.transform = CGAffineTransformMakeScale(1.1, 1.1))
,然后回到所需的尺寸(view.transform = CGAffineTransformMakeScale(0.1, 0.1))
,或者添加更多的步骤以获得更大的反弹。
为了移动它,实现触摸方法并随着手指的移动改变视图的框架。
编辑:这里是自定义UIAlertView样UIView的示例代码。
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
然后,要显示该警报的位置,您需要:
#import "MyAlertView.h"
和:
MyAlertView *alert = [[MyAlertView alloc] initWithFrame:CGRectMake(20, 100, 280, 100)]; [viewFromWhichYouWillShowTheAlert addSubview:alert]; [alert release];
那么你使用[alert show];
显示它[alert show];
,使用[alert hide];
,或者使用[alert toggle];
您还可以在点击并拖动(除closuresbutton外的任何地方)时移动它。 我希望这足以让你开始。 如果你需要解释代码的任何部分,只要问。
哦,注意我把这个视图的颜色设置为白色,所以如果你在其他白色视图上显示它,你不会真的看到它,所以只要改变任何视图的背景颜色:)
您可以简单地按照以下步骤获取
- 创build尺寸为320 * 480的UIview(ViewA),使其覆盖iPhone的整个屏幕,并将背景设置为clearColor。这将作为超级视图用于我们的目的;
- 创build另一个尺寸为320 * 480的UIView(ViewB),背景颜色设置为黑色,不透明度设置为40%。 现在你可以在ViewB上添加任何视图。
- 现在将ViewB添加到ViewA。
最后你可以在任何需要的地方展示这个视图。 效果会是,ViewA将覆盖Background viewController,ViewB将服务器作为背景视图控制器的抑制效果,而B上的视图就是你会看到的UIElement。
对于animation效果,您可以在ViewB上的UIElement上使用一些基本的animation代码。