2011-08-06 29 views

cevap

24

Animate görünümü Diyelim ki o (view.transform = CGAffineTransformMakeScale(1.1, 1.1)) gibi, sonra tekrar istediğiniz boyuta olmak istiyorum o zaman biraz daha büyük bir şeye UIView animasyon (using blocks veya older api) bazı gerçekten küçük boyutundan

(view.transform = CGAffineTransformMakeScale(0.1, 0.1) gibi) kullanılarak dönüşümü .. (view.transform = CGAffineTransformMakeScale(0.1, 0.1)) veya daha büyük sıçrama için fazla adım eklemek

ve etrafında hareket ettirmek için, touch methods uygulamak ve parmağınızı hareket ettirdikçe görünümün çerçeveyi değiştirmek

Düzenleme: burada özel UIAlertView benzeri UIView için örnek kod.

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 
Sonra

bu uyarıyı göstermek istediğiniz yere şunlar gereklidir:

#import "MyAlertView.h" 

ve:

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

o zaman [alert hide]; kullanarak gizlemek, [alert show]; kullanarak veya dokunup sürükleme yaptığınızda da (yere yakın butonuna hariç) etrafında hareket edebilir [alert toggle];

kullanarak geçiş göstermektedir. Umarım başlaman için bu yeterlidir. Kodun herhangi bir kısmı için açıklama gerekiyorsa sadece sorun.

Oh, ve bu yüzden sadece herhangi bir görünümde arka plan rengini değiştirmek, sana diğer beyaz görünümünün üstünde göstermek eğer, gerçekten gözle görülmeyecek kadar beyaza Bu görüşün rengini ayarlamak fark :)

0

sadece aşağıdaki adımlardan

aşağıdaki o edinebilirler
  1. oluşturma boyutu 320 * 480 arasında UIView (viewA), bu gibi süper görünümü hizmet verecek clearColor.This ayarlı arka plan ile iPhone tüm ekranı karşılayacak şekilde Amacımız;
  2. Arka plan rengi siyah olarak ayarlanmış ve opaklığı% 40 olan 320 * 480 boyutunda başka bir UIView (ViewB) oluşturun. 3.Now ViewB'de herhangi bir görünüm ekleyebilirsiniz.
  3. Şimdi ViewB'yi ViewA'ya ekleyin.

Son olarak, istediğiniz zaman bu görünümü sunabilirsiniz. Etki, ViewA, Arkaplan viewController'ı kapsayacak, ViewB, arka plan görünümü denetleyicisi için bastırıcı etki olarak sunucu ve B'deki görünümler, göreceğiniz UIElement'tir.

Animasyon efekti için, UIElement on ViewB'de bazı temel animasyon kodlarını kullanabilirsiniz.