类似 UIAlertView 的 UIView 弹出窗口 [英] UIView Popup like UIAlertView

查看:18
本文介绍了类似 UIAlertView 的 UIView 弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个可以像 UIAlertView 一样弹出并且可以移动的 UIView.

I Want a UIView which can popup like UIAlertView and also can move.

有什么帮助吗??

谢谢.

推荐答案

使用 UIView 动画 (使用块较旧的 api)

Animate the view's transform using UIView animation (using blocks or older api)

从一些非常小的尺寸(比如 view.transform = CGAffineTransformMakeScale(0.1, 0.1))到比你想要的更大一点的尺寸(比如 view.transform = CGAffineTransformMakeScale(1.1, 1.1)) ,然后回到想要的尺寸(view.transform = CGAffineTransformMakeScale(0.1, 0.1)),或者增加更多的步骤以获得更大的反弹.

from some really small size (like view.transform = CGAffineTransformMakeScale(0.1, 0.1)) to something a little bigger then you want it to be (like view.transform = CGAffineTransformMakeScale(1.1, 1.1)), then back to the desired size (view.transform = CGAffineTransformMakeScale(0.1, 0.1)), or add more steps for bigger bounce.

为了移动它,实现 触摸方法 并随着手指的移动改变视图的框架.

And for moving it around, implement the touch methods and change the view's frame as the finger moves.

这是自定义类似 UIAlertView 的 UIView 的示例代码.

here is the sample code for custom UIAlertView-like UIView.

MyAlertView.h:

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:

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 where you want to show that alert, you need:

#import "MyAlertView.h"

和:

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

然后你用[alert show];显示,用[alert hide];隐藏,或者用[alert toggle];切换>

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

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

You can also move it around when you tap and drag (anywhere except on the close button). I hope this is enough to get you started. If you need explanation for any part of code just ask.

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

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

这篇关于类似 UIAlertView 的 UIView 弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆