图片分离--分成两片

本文介绍了一种使用Objective-C在iOS应用中实现图片上下部分裁剪显示的方法,并通过触摸事件触发视图的平移动画效果。

@interface ViewController ()

@property (nonatomic,strong)UIImageView *topImageView;

@property(nonatomic,strong)UIImageView *bottomImageView;

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(100, 300, 100, 30)];

    label.textColor=[UIColor redColor];

    label.textAlignment=NSTextAlignmentCenter;

    label.text=@"你看到我了" ;

    [self.view addSubview:label];

    

    UIImage *imageTop=[UIImage imageNamed:@"1111.jpg"];

    //根据创建的image 进行区域裁剪(关键代码)

    CGImageRef refTopImg=CGImageCreateWithImageInRect(imageTop.CGImage, CGRectMake(0, 0, imageTop.size.width, imageTop.size.height/2));

   _topImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 375, 667/2)];

    _topImageView.image=[UIImage imageWithCGImage:refTopImg];

    [self.view addSubview:_topImageView];

    

    CGImageRef refBottomImg=CGImageCreateWithImageInRect(imageTop.CGImage, CGRectMake(0, imageTop.size.height/2, imageTop.size.width, imageTop.size.height/2));

    _bottomImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 667/2, 375, 667/2)];

    _bottomImageView.image=[UIImage imageWithCGImage:refBottomImg];

    

    [self.view addSubview:_bottomImageView];

    

    

    

    

    

}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    [UIView animateWithDuration:1 animations:^{

       

        _topImageView.frame=CGRectMake(0, -667/2, 375, 667/2);

        

        _bottomImageView.frame=CGRectMake(0, 667, 375, 667/2);

    }];

}

转载于:https://www.cnblogs.com/daxueshan/p/6243706.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值