2011-08-06 13 views

उत्तर

24

चेतन दृश्य के कुछ थोड़ा बड़ा करने के लिए UIView एनीमेशन (using blocks या older 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. बनाएं UIView आकार 320 * 480 के (ViewA), इतना है कि यह clearColor.This करने के लिए सेट पृष्ठभूमि के साथ iPhone के पूरे स्क्रीन को कवर किया जाएगा के लिए के रूप में सुपर दृश्य में काम करेगा हमारा उद्देश्य;
  2. आकार 320 * 480 के एक और UIView (ViewB) को पृष्ठभूमि रंग के साथ काला और अस्पष्टता 40% तक सेट करें। 3. अब आप व्यूबी पर कोई भी दृश्य जोड़ सकते हैं।
  3. अब व्यूए को व्यूए जोड़ें।

अंत में आप इस दृश्य को कभी भी प्रस्तुत कर सकते हैं। प्रभाव होगा, व्यूए पृष्ठभूमि दृश्य नियंत्रक को कवर करेगा, व्यूबी पृष्ठभूमि पृष्ठभूमि नियंत्रक के लिए प्रभाव को दबाने के रूप में सर्वर करेगा और बी पर विचार UIElement देखेंगे जो आप देखेंगे।

एनीमेशन प्रभाव के लिए आप व्यूबी पर UIElement पर कुछ मूल एनीमेशन कोड का उपयोग कर सकते हैं।