IOS開發基礎知識--片段25,ios基礎知識--片段
1:使用@protocol實現delegate和datasource模式
#import <UIKit/UIKit.h>@protocol MyViewDataSource,MyViewDelegate;@interface myView : UIView<UIAlertViewDelegate>@property(nonatomic,assign)id<MyViewDelegate> myViewDelegate;@property(nonatomic,assign)id<MyViewDataSource> myViewDataSource;-(void)myShowAlert;@end@protocol MyViewDelegate <NSObject>@optional-(void)alertDidPop:(myView *)myView;-(void)alertConfirmShow:(myView *)myView clickedButtonAtIndex:(NSInteger)buttonIndex;@end@protocol MyViewDataSource <NSObject>@optional-(NSString *)textOfAlert:(myView *)myView;@required- (NSUInteger)numberOfItemsInMyView:(myView *)myView;@required@end
#import "myView.h"@implementation myView-(void)myShowAlert{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"測試執行個體" message:@"message" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定",nil]; alert.message = [self.myViewDataSource textOfAlert:self]; [alert show];}- (void)didPresentAlertView:(UIAlertView *)alertView{ [self.myViewDelegate alertDidPop:self];}-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ [self.myViewDelegate alertConfirmShow:self clickedButtonAtIndex:buttonIndex];}@end
使用方式:
#import "myView.h"@interface ViewController ()<MyViewDataSource,MyViewDelegate>@end
- (void)viewDidLoad { [super viewDidLoad]; myView *myVw = [[myView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)]; myVw.myViewDataSource = self; myVw.myViewDelegate = self; [self.view addSubview:myVw]; [myVw myShowAlert];}
代理實現的方法:- (void)alertDidPop:(UIView *)myView{ myView.backgroundColor = [UIColor yellowColor];}-(NSString *)textOfAlert:(myView *)myView{ return @"資訊";}-(void)alertConfirmShow:(myView *)myView clickedButtonAtIndex:(NSInteger)buttonIndex{ NSLog(@"你選中了%d",buttonIndex);}
2:動畫 UIView animateWithDuration 使用詳解
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
duration為動畫持續的時間。animations為動畫效果的代碼塊
可設動作屬性:
- frame
- bounds
- center
- transform
- alpha
- backgroundColor
- contentStretch
例如一個視圖淡出螢幕,另外一個視圖出現的代碼
[UIView animateWithDuration:1.0 animations:^{ firstView.alpha = 0.0; secondView.alpha = 1.0;}];
連續動畫(可以在completion代碼塊中添加動畫):
[UIView animateWithDuration:2.0 animations:^{ oldImageView.alpha = 0.0; newImageView.alpha = 1.0; //imageView.center = CGPointMake(500.0, 512.0); } completion:^(BOOL finished){ [UIView animateWithDuration:4.0 animations:^{ newImageView.center = CGPointMake(500.0, 512.0); }]; }];
從上往下一個動作(預設是左上方,把要改變的值放在animations裡):
-(UIView *)myView{ if (!_myView) { _myView=[[UIView alloc]initWithFrame:CGRectZero]; _myView.backgroundColor=[UIColor redColor]; } return _myView;}- (IBAction)BtnAction:(id)sender { self.myView.frame = CGRectMake(0,44, 320, 0); [self.view addSubview:self.myView]; [UIView animateWithDuration:0.3 animations:^{ self.myView.backgroundColor=[UIColor redColor]; self.myView.frame = CGRectMake(0,44, 320, 100); } completion:^(BOOL finished) { }];}
3:UIView 的旋轉和縮放
label.transform = CGAffineTransformMakeRotation(90 *M_PI / 180.0);//順時針旋轉 90 度度 label.transform = CGAffineTransformMakeRotation(180 *M_PI / 180.0);//順時針 旋轉180度 label.transform = CGAffineTransformMakeRotation(270 *M_PI / 180.0);//順時針旋轉270度CGAffineTransform transform = label.transform;transform = CGAffineTransformScale(transform, 2,0.5);//前面的2表示橫向放大2倍,後邊的0.5表示縱向縮小一半 label.transform = transform;