2011-08-06 26 views
8

Tôi muốn có một UIView có thể bật lên như UIAlertView và cũng có thể di chuyển.UIView Popup như UIAlertView

Mọi trợ giúp xin vui lòng ??

Cảm ơn bạn.

Trả lời

24

Animate của xem chuyển đổi sử dụng UIView hoạt hình (using blocks hoặc older api)

từ một số kích thước rất nhỏ (như view.transform = CGAffineTransformMakeScale(0.1, 0.1)) để một cái gì đó lớn hơn một chút thì bạn muốn nó được (như view.transform = CGAffineTransformMakeScale(1.1, 1.1)), sau đó trở lại kích thước mong muốn .. (view.transform = CGAffineTransformMakeScale(0.1, 0.1)), hoặc bổ sung thêm bước này cho thư bị trả lại lớn hơn

và để di chuyển nó xung quanh, thực hiện touch methods và thay đổi khung của xem như di chuyển ngón tay

Edit: đây là mã mẫu cho UIView tùy chỉnh UIAlertView.

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 

Sau đó, nơi bạn muốn hiển thị cảnh báo đó, bạn cần:

#import "MyAlertView.h" 

và:

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

sau đó bạn hiển thị nó bằng cách sử [alert show];, ẩn bằng [alert hide];, hoặc chuyển đổi sử dụng [alert toggle];

Bạn cũng có thể di chuyển nó xung quanh khi bạn nhấn và kéo (bất cứ nơi nào trừ vào nút đóng). Tôi hy vọng điều này là đủ để giúp bạn bắt đầu. Nếu bạn cần giải thích cho bất kỳ phần nào của mã chỉ cần hỏi.

Oh, và nhận ra rằng tôi đặt màu của điểm này sang màu trắng vì vậy nếu bạn hiển thị nó trên đầu trang của cái nhìn trắng khác, bạn sẽ không thực sự nhìn thấy nó, vì vậy chỉ cần thay đổi màu nền của bất kỳ xem :)

0

Bạn có thể có được mà chỉ đơn giản làm theo các bước sau

  1. Tạo UIView (ViewA) kích thước 320 * 480, do đó nó sẽ bao gồm toàn bộ màn hình của iPhone với nền thiết lập để clearColor.This sẽ phục vụ như siêu xem cho mục đích của chúng tôi;
  2. Tạo một UIView (ViewB) khác có kích thước 320 * 480 với màu nền được đặt thành màu đen và độ mờ thành 40%. 3.Bây giờ, bạn có thể thêm bất kỳ chế độ xem nào trên ViewB.
  3. Bây giờ, hãy thêm ViewB vào ViewA.

Cuối cùng, bạn có thể Trình bày chế độ xem này khi cần thiết. Hiệu ứng sẽ là, ViewA sẽ bao gồm nền ViewController, ViewB sẽ máy chủ như là hiệu ứng đàn áp cho điều khiển xem nền và quan điểm trên B là UIElement bạn sẽ thấy.

Đối với hiệu ứng Hoạt hình, bạn có thể sử dụng một số mã hoạt ảnh cơ bản trên UIElement trên ViewB.