iUs*_*ser 8 iphone uiview uialertview ios xcode4
我想要一个可以像UIAlertView一样弹出的UIView,也可以移动.
有什么帮助吗?
谢谢.
Fil*_*lic 24
使用UIView动画(使用块或更旧的api)为视图的变换设置动画
从一些非常小的尺寸(比如view.transform = CGAffineTransformMakeScale(0.1, 0.1))到你想要的更大的东西(比如view.transform = CGAffineTransformMakeScale(1.1, 1.1)),然后回到所需的尺寸(view.transform = CGAffineTransformMakeScale(0.1, 0.1)),或者添加更多的步骤以获得更大的反弹.
并且为了移动它,实现触摸方法并在手指移动时改变视图的框架.
编辑:这是自定义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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
然后,您想要显示该警报的位置,您需要:
#import "MyAlertView.h"
Run Code Online (Sandbox Code Playgroud)
和:
MyAlertView *alert = [[MyAlertView alloc] initWithFrame:CGRectMake(20, 100, 280, 100)];
[viewFromWhichYouWillShowTheAlert addSubview:alert];
[alert release];
Run Code Online (Sandbox Code Playgroud)
然后你使用[alert show];,隐藏使用[alert hide];或切换使用它来显示它[alert toggle];
您可以在点击并拖动时移动它(除了关闭按钮之外的任何地方).我希望这足以让你开始.如果您需要解释代码的任何部分,请询问.
哦,请注意我将此视图的颜色设置为白色,因此如果您将其显示在其他白色视图的顶部,您将无法真正看到它,因此只需更改任何视图的背景颜色:)
| 归档时间: |
|
| 查看次数: |
13818 次 |
| 最近记录: |