我想要一个可以像UIAlertView一样弹出并且能够移动的UIView组件。请问有什么帮助吗?
谢谢。
谢谢。
view.transform = CGAffineTransformMakeScale(0.1, 0.1)
)到稍微大一点的大小(例如view.transform = CGAffineTransformMakeScale(1.1, 1.1)
),然后回到所需的大小(view.transform = CGAffineTransformMakeScale(0.1, 0.1))
,或者添加更多步骤以获得更大的弹跳效果。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];
切换它。
您还可以在轻击和拖动时将其移动(除了关闭按钮以外的任何位置)。 希望这足以让您入门。 如果您需要对代码的任何部分进行解释,请随时提问。
哦,注意我将此视图的颜色设置为白色,因此如果您将其显示在其他白色视图的顶部,您实际上看不到它,所以只需更改任何视图的背景颜色即可 :)
按照以下步骤即可实现:
最后,您可以在需要的地方呈现此视图。效果是,ViewA将覆盖背景viewController,ViewB将作为背景viewController抑制效果,而B上的视图是您将看到的UIElement。
对于动画效果,您可以在ViewB上的UIElement上使用一些基本的动画代码。