2011-08-06 54 views

回答

24

動畫視圖的轉換到更大的東西一點點使用的UIView動畫(using blocksolder api

從一些非常小的尺寸(如view.transform = CGAffineTransformMakeScale(0.1, 0.1)),那麼你希望它是(像view.transform = CGAffineTransformMakeScale(1.1, 1.1)),然後再返回到所需的大小。(view.transform = CGAffineTransformMakeScale(0.1, 0.1)),或添加更多的步驟,更大的反彈

而四處移動它,實現touch methods和更改視圖的幀作爲移動手指

編輯:這裏是自定義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];,或者使用[alert toggle];

當你點擊並拖動您還可以將它周圍(除任何地方上的關閉按鈕)切換。我希望這足以讓你開始。如果您需要解釋代碼的任何部分,請查詢。

哦,注意我設置這種觀點爲白色,如果你表現出它在其他白的觀點之上,你不會真正看到它的顏色,所以只是改變任何視圖的背景顏色:)

0

您可以獲取,簡單地按照以下步驟

  1. 創建大小爲320 * 480的的UIView(ViewA),這樣它會覆蓋背景設置爲clearColor.This iPhone的整個屏幕將服務於超級視圖我們的目的;
  2. 創建另一個尺寸爲320 * 480的UIView(ViewB),背景顏色設置爲黑色,不透明度設置爲40%。 3.現在您可以在ViewB上添加任何視圖。
  3. 現在將ViewB添加到ViewA。

最後,您可以在任何需要的地方展示此視圖。效果將是,ViewA將覆蓋Background viewController,ViewB將服務器作爲背景視圖控制器的抑制效果,而B上的視圖是您將看到的UIElement。

對於動畫效果,您可以在ViewB上的UIElement上使用一些基本的動畫代碼。