UIView Popup就像UIAlertView一样

时间:2011-08-06 09:01:27

标签: iphone ios uiview xcode4 uialertview

我想要一个可以像UIAlertView一样弹出的UIView,也可以移动。

请帮忙吗?

谢谢。

2 个答案:

答案 0 :(得分:24)

使用UIView动画(using blocksolder api

为视图的变换设置动画

从一些非常小的尺寸(如view.transform = CGAffineTransformMakeScale(0.1, 0.1))到你想要的更大的东西(如view.transform = CGAffineTransformMakeScale(1.1, 1.1)),然后回到所需的尺寸(view.transform = CGAffineTransformMakeScale(0.1, 0.1)),或添加更多更大的反弹步骤。

要移动它,请实施touch methods并在手指移动时更改视图的框架。

编辑:这是自定义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 hide];隐藏,或使用[alert toggle];切换

您可以在点击并拖动时移动它(除了关闭按钮之外的任何地方)。我希望这足以让你开始。如果您需要解释代码的任何部分,请询问。

哦,请注意我将此视图的颜色设置为白色,因此如果您将其显示在其他白色视图的顶部,您将无法真正看到它,因此只需更改任何视图的背景颜色:)

答案 1 :(得分:0)

您只需按照以下步骤即可获得

  1. 创建尺寸为320 * 480的UIview(ViewA),以便覆盖iPhone的整个屏幕,背景设置为clearColor。这将作为我们目的的超级视图;
  2. 创建另一个大小为320 * 480的UIView(ViewB),背景颜色设置为黑色,不透明度设置为40%。 3.现在可以在ViewB上添加任何视图。
  3. 现在将ViewB添加到ViewA。
  4. 最后,您可以根据需要提供此视图。效果将是,ViewA将覆盖Background viewController,ViewB将服务器视为背景视图控制器的抑制效果,B上的视图是您将看到的UIElement。

    对于动画效果,您可以在ViewB上的UIElement上使用一些基本动画代码。