IOS開發基礎知識--片段25,ios基礎知識--片段

來源:互聯網
上載者:User

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;

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.