UIView 弹出窗口,如 UIAlertView

发布于 2024-11-28 03:00:37 字数 75 浏览 0 评论 0原文

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

有什么帮助吗?

谢谢。

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

Any help please??

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千仐 2024-12-05 03:00:37

使用 UIView 动画对视图的变换进行动画处理 (使用块旧版 API)

从一些非常小的尺寸(例如 view.transform = CGAffineTransformMakeScale(0.1, 0.1))到更大一点的尺寸然后你希望它是(例如view.transform = CGAffineTransformMakeScale(1.1, 1.1)),然后回到所需的大小(view.transform = CGAffineTransformMakeScale(0.1, 0.1)) code>,或添加更多步骤以获得更大的弹跳。

要移动它,请实现 触摸方法并随着手指的移动改变视图的框架。

编辑:这里是自定义 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]; 隐藏它,或使用[警报切换]进行切换;

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

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

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

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.

Edit: here is the sample code for custom UIAlertView-like 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

Then where you want to show that alert, you need:

#import "MyAlertView.h"

and:

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

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 :)

遇见了你 2024-12-05 03:00:37

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

  1. 创建尺寸为 320*480 的 UIview (ViewA),使其覆盖 iPhone 的整个屏幕,背景设置为clearColor。这将作为我们目的的超级视图;
  2. 创建另一个大小为 320*480 的 UIView (ViewB),背景颜色设置为黑色,不透明度设置为 40%。
    3.现在您可以在ViewB上添加任何视图。
  3. 现在将 ViewB 添加到 ViewA。

最后,您可以在需要时呈现此视图。效果是,ViewA 将覆盖背景视图控制器,ViewB 将作为背景视图控制器的抑制效果,B 上的视图是您将看到的 UIElement。

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

You can acquire that simply following the following steps

  1. Create UIview (ViewA)of size 320*480 , so that it will cover whole screen of iPhone with background set to clearColor.This will serve as super view for our purpose;
  2. Create another UIView (ViewB) of size 320*480 with background color set to black and opacity to 40%.
    3.Now you can add any view on ViewB.
  3. Now add ViewB to ViewA.

Finally you can Present this view where ever required. The effect will be, ViewA will cover the Background viewController, ViewB will server as suppressing effect for background view controller and views on B are the UIElement you will see.

For Animation effect you can use some basic animation code on the UIElement on ViewB.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文