UIView Popup就像UIAlertView一样 [英] UIView Popup like UIAlertView

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

问题描述

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

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

任何帮助都可以吗?

谢谢。

推荐答案

使用UIView动画为视图的变换设置动画(使用块older 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 :)

这篇关于UIView Popup就像UIAlertView一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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