ios学习第六天(四)美丽的自定义View

先看效果,如果使用线程控制,树的颜色将会变化,使用的图片如果足够精致,效果将会更好。


图片中这棵树,不是背景图片,是自定义View+图片共同效果,借助上一篇的彩色效果,加上一颗树的图片即可完成,看代码吧,太晚了,不解释。

//
//  MyMagicalView.h
//  MyUIView
//
//  Created by Moluth on 17/4/12.
//  Copyright (c) 2017年 Moluth. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MyMagicalView : UIView{
@public double tr,tg,tb;
@public void* imageData;
@public int width,height;
}

@end

//
//  MyMagicalView.m
//  MyUIView
//
//  Created by Moluth on 17/4/12.
//  Copyright (c) 2017年 Moluth. All rights reserved.
//

#import "MyMagicalView.h"

@implementation MyMagicalView
- (id)initWithFrame : (CGRect)frame
{
    self = [super initWithFrame:frame];
    UIImage *img=[UIImage imageNamed:@"tree.jpg"];
    [self getImageData:img];
    return self;
}

-(void)drawRect:(CGRect)rect{
    
    // 获取图形绘制上下文
    
    
    
    int xa=round(rect.origin.x);
    int xb=round(rect.origin.x+rect.size.width);
    int ya=round(rect.origin.y);
    int yb=round(rect.origin.y+rect.size.height);
    
    unsigned char* data=(unsigned char*)imageData;

    CGContextRef context = UIGraphicsGetCurrentContext();
    double r,g,b;
    for(int x=xa;x<xb;x++){
        for(int y=ya;y<yb;y++){
            int c=data[((height-y)*width+x)*4+1];
            if(c>235)continue;
            
            r=(sin(x*0.0017-y*0.0005+tr+34.1)+1.0)/2.0;
            g=(sin((xb-x)*0.0009+y*0.0015+tg)+1.0)/2.0;
            b=(sin(x*0.0047+(yb-y)*0.0001+tb)+1.0)/2.0;
            CGContextSetRGBFillColor (context,  r, g, b, 1.0);//设置填充颜色
            CGContextFillRect(context,CGRectMake(x, y, 1, 1));
            
        }
    }
    
}
- (void)getImageData:(UIImage*)image
{
    width=image.size.width;
    height=image.size.height;
    if (imageData == NULL)
        imageData = malloc(4 * image.size.width * image.size.height);
    
    CGColorSpaceRef cref = CGColorSpaceCreateDeviceRGB();
    CGContextRef gc = CGBitmapContextCreate(imageData,
                                            image.size.width,image.size.height,
                                            8,image.size.width*4,
                                            cref,kCGImageAlphaPremultipliedFirst);
    CGColorSpaceRelease(cref);
    UIGraphicsPushContext(gc);
    
    [image drawAtPoint:CGPointMake(0.0f, 0.0f)];
    
    UIGraphicsPopContext();
    CGContextRelease(gc);
}



@end

///今天到此为止了,结束,bye







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在iOS的画中画窗口中添加自定义视图,您需要按照以下步骤进行操作: 1.创建一个自定义视图,继承自UIView类。 2.在需要使用画中画功能的UIViewController中,添加以下代码以启用画中画: ```swift override func viewDidLoad() { super.viewDidLoad() // 允许画中画 self.isModalInPresentation = true } ``` 3.在需要添加自定义视图的UIViewController中,创建一个AVPlayerViewController实例,并将其添加到视图层次结构中: ```swift // 创建一个AVPlayerViewController实例 let playerViewController = AVPlayerViewController() // 添加到视图层次结构中 addChild(playerViewController) view.addSubview(playerViewController.view) playerViewController.didMove(toParent: self) ``` 4.在AVPlayerViewController实例中,使用AVPictureInPictureController的`playerViewController(_:restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:)`方法,在画中画结束后恢复自定义视图: ```swift // 在画中画结束后恢复自定义视图 func playerViewController(_ playerViewController: AVPlayerViewController, restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void) { // 创建自定义视图 let customView = CustomView(frame: playerViewController.view.bounds) // 添加到视图层次结构中 playerViewController.view.addSubview(customView) playerViewController.view.bringSubviewToFront(customView) completionHandler(true) } ``` 请注意,您需要在info.plist文件中添加“Supported external accessory protocols”键,并将其设置为“com.apple.avfoundation.tunnel”的值,以便在iOS 14及更高版本中启用画中画功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值