IOS开发UI阶段 第六周

8月13日
自习一天,复习UI前面的课程

8月14日
单元格的复用、单元格的常用属性、辅助图标、自适应高度

8月15日
单元格的定制的三种方法

8月16日
做课后作业,自习

8月17日
触摸与手势

8月18日
UIScrollView

综合性的示例:模拟相册

 重要代码:
1、在根视图控制中:
@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

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


    //设置视图控制器的滑动视图大小,为yes时,自动从statuBar、navigationBar下面显示

    self.automaticallyAdjustsScrollViewInsets = NO;


    self.title = @"图片浏览";

    

    //创建滑动视图

    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];


    scrollView.delegate = self;


    for (int i = 0; i < 5; i ++) {

        PhotoScrollView *photoScrollView = [[PhotoScrollView alloc]initWithFrame:CGRectMake(scrollView.bounds.size.width * i, 0, scrollView.bounds.size.width, scrollView.bounds.size.height)];

        photoScrollView.tag = i + 100;

        

        //传值

        photoScrollView.img = [UIImage imageNamed:[NSString stringWithFormat:@"%d.JPG", i]];

        

        [scrollView addSubview:photoScrollView];

        

    }

    

    //取消滑动条的显示

    scrollView.showsHorizontalScrollIndicator = NO;

    

    //设置分页

    scrollView.pagingEnabled = YES;

    

    //设置内容尺寸

    scrollView.contentSize = CGSizeMake(scrollView.bounds.size.width * 5,scrollView.bounds.size.height);


    [self.view addSubview:scrollView];


}



#pragma mark -UIScrollViewDelegate


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    //获取当前下标

    NSInteger nowIndex = scrollView.contentOffset.x / scrollView.bounds.size.width;

    

    if (nowIndex != _lastIndex) {

        PhotoScrollView *photoScrollView = (PhotoScrollView *)[scrollView viewWithTag:_lastIndex + 100];

        

        //还原

        photoScrollView.zoomScale = 1;

    }

    

    _lastIndex = nowIndex;

}

}


@end

2、在滑动视图类中

@implementation PhotoScrollView


- (instancetype)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        

        _imgView = [[UIImageView alloc]initWithFrame:self.bounds];

        

        [self addSubview:_imgView];

        

        //设置缩放的最大倍数

        self.maximumZoomScale = 3;

        self.minimumZoomScale = 1;

        

        //取消滚动条

        self.showsHorizontalScrollIndicator = NO;

        self.showsVerticalScrollIndicator = NO;

        

        //设置代理

        self.delegate = self;

        

        //创建单击手势

        UITapGestureRecognizer *navTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(navTapAction:)];

        [self addGestureRecognizer:navTap];

        

        //创建双击手势

        UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];

        doubleTap.numberOfTapsRequired = 2;

        [self addGestureRecognizer:doubleTap];

        

        [navTap requireGestureRecognizerToFail:doubleTap];

        

    }

    return self;

}


- (void)setImg:(UIImage *)img

{

    if (_img != img) {

        _img = img;

        

        _imgView.image = self.img;

    }

    

}


- (void)navTapAction:(UITapGestureRecognizer *)tap

{

    //隐藏、显示导航栏

    BOOL show = !self.viewController.navigationController.navigationBarHidden;

    

    [self.viewController.navigationController setNavigationBarHidden:show animated:YES];

    

    //隐藏、显示电池条

    [[UIApplication sharedApplication] setStatusBarHidden:show withAnimation:UIStatusBarAnimationFade];

}


- (void)tapAction:(UITapGestureRecognizer *)tap

{

    if (self.zoomScale > 1) {

        //缩小

        [self setZoomScale:1 animated:YES];

    }else{

        //放大

        [self setZoomScale:3 animated:YES];

    }

    

}



#pragma mark -UIScrollViewDelegate


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

{

    return _imgView;

}

@end

3、通过响应者链找根视图控制器,创建uiview的类目,在里面写以下方法

-(UIViewController *)viewController

{

    UIResponder *next = self.nextResponder;

    

    do {

        if ([next isKindOfClass:[UIViewController class]]) {

            return (UIViewController *)next;

        }

        next = next.nextResponder;

    } while (next);

    

    return nil;

}






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS开发中,我们可以通过添加水印来给UIView添加一些额外的视觉效果。下面是一个实现的示例代码: ```swift // 创建带有水印的UIView子类 class WatermarkView: UIView { let watermarkText: String // 水印文本 init(frame: CGRect, watermarkText: String) { self.watermarkText = watermarkText super.init(frame: frame) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func draw(_ rect: CGRect) { super.draw(rect) // 设置水印文本的属性 let attributes: [NSAttributedString.Key : Any] = [ .font: UIFont.systemFont(ofSize: 16), .foregroundColor: UIColor.white.withAlphaComponent(0.5) ] // 绘制水印文本 let watermarkString = NSAttributedString(string: watermarkText, attributes: attributes) watermarkString.draw(at: CGPoint(x: 10, y: rect.height - 30)) } } // 在ViewController中使用WatermarkView class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // 创建一个带有水印的UIView并添加到当前视图控制器视图层级中 let watermarkView = WatermarkView(frame: view.bounds, watermarkText: "Watermark") view.addSubview(watermarkView) } } ``` 在上面的示例代码中,我们创建了一个名为`WatermarkView`的UIView子类。在其`draw(_ rect:)`方法中,我们使用`NSAttributedString`绘制了带有水印文本的视图。我们可以调整文本的字体和颜色等属性来满足需求。 在ViewController中,我们使用`WatermarkView`类创建了一个带有水印的UIView实例,并将其添加到当前视图控制器的视图层级中。通过这样的方式,我们可以在应用中的任何需要的位置添加水印效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值