IOS开发基础知识--碎片25

来源:互联网 时间:1970-01-01

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;@[email protected] MyViewDelegate <NSObject>@optional-(void)alertDidPop:(myView *)myView;-(void)alertConfirmShow:(myView *)myView clickedButtonAtIndex:(NSInteger)buttonIndex;@[email protected] MyViewDataSource <NSObject>@optional-(NSString *)textOfAlert:(myView *)myView;@required- (NSUInteger)numberOfItemsInMyView:(myView *)myView;@[email protected]
#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;

 



相关阅读:
Top