码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2016-08-22 21:27:38      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

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;
技术分享

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

标签:

原文地址:http://www.cnblogs.com/LiLihongqiang/p/5796870.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!