zoukankan      html  css  js  c++  java
  • iOS UIScrollView初体验

    //
    //  AppDelegate.m
    //  wzy_ios_demo
    //
    //  Created by admin on 2020/12/8.
    //
    
    #import "AppDelegate.h"
    #import "MyUiView.h"
    #import "ViewController.h"
    @interface AppDelegate ()
    
    @end
    
    @implementation AppDelegate
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window = [[UIWindow alloc] initWithFrame:
                 [[UIScreen mainScreen] bounds]];
        
        CGRect firstFrame = self.window.bounds;
        CGRect bigRect = self.window.bounds;
        bigRect.size.width *= 2;
        
        //两个view,secondView的起始点在firstView右边(即屏幕右侧)
        MyUiView *firstView = [[MyUiView alloc] initWithFrame: firstFrame];
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame: firstFrame];
        firstFrame.origin.x += firstFrame.size.width;
        MyUiView *secondView = [[MyUiView alloc] initWithFrame: firstFrame];
        
        //自动吸附到页面对齐
        scrollView.pagingEnabled = YES;
        //指定实际的范围
        scrollView.contentSize = bigRect.size;
        //给scrollView添加两个子view
        [scrollView addSubview: firstView];
        [scrollView addSubview: secondView];
        [self.window addSubview: scrollView];
        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
        
        //将view绑定到viewController上
        ViewController *controller = [ViewController new];
        [controller.view addSubview: scrollView];
        
        self.window.rootViewController = controller;
        return YES;
    }
    
    @end
    //
    //  MyUiView.m
    //  wzy_ios_demo
    //
    //  Created by admin on 2020/12/8.
    //
    
    #import "MyUiView.h"
    
    @interface MyUiView ()  //分类
    
    @property (strong, nonatomic) UIColor *circleColor;
    
    @end
    
    @implementation MyUiView
    
    -(instancetype) initWithFrame:(CGRect)frame {
        self = [super initWithFrame: frame];
        if (self) {
            self.backgroundColor = [UIColor clearColor];
            self.circleColor = [UIColor greenColor];
            self.userInteractionEnabled = YES;
        }
        
        return self;
    }
    
    -(void) drawRect:(CGRect)rect {
        CGRect bounds = self.bounds;
        CGPoint center;
        center.x = bounds.origin.x + bounds.size.width / 2.0;
        center.y = bounds.origin.y + bounds.size.height / 2.0;
        float radius = MIN(bounds.size.width, bounds.size.height) / 2.0;    //取小的,防止竖屏能放下但横屏放不下
        UIBezierPath *path = [[UIBezierPath alloc] init];
        [path addArcWithCenter: center radius: radius startAngle: 0 endAngle: 2.0 * M_PI clockwise: YES];
        path.lineWidth = 20;
        [self.circleColor setStroke];
        [path stroke];
    }
    
    -(void) setCircleColor:(UIColor *)circleColor {
        _circleColor = circleColor;
        [self setNeedsDisplay];
    }
    
    -(void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
        NSLog(@"%@ was touched", self);
        float red = (arc4random() % 100) / 100.0;
        float blue = (arc4random() % 100) / 100.0;
        float green = (arc4random() % 100) / 100.0;
        UIColor *randomColor = [UIColor colorWithRed: red green:green blue:blue alpha: 1.0];
        
        self.circleColor = randomColor;
    }
    @end

    效果:可以左右拖动,会自动吸附,就和flutter的PageView效果差不多

  • 相关阅读:
    Flash特效 嘿嘿
    惨,被骗了20年
    “不允许对64位应用程序进行修改”的解决方法 “Changes to 64bit applications are not allowed.”
    清除Sql Server数据库日志
    .Net 序列化(去除默认命名空间,添加编码)
    【Vegas原创】X connection to localhost:11.0 broken (explicit kill or server shutdown)解决方法
    【Vegas原创】通过WMIC命令远程打开远程计算机的远程桌面(Remote Desktop)功能
    【Vegas原创】ORA12638: 身份证明检索失败的解决办法
    【Vegas原创】SQL Server 阻止了对组件 'SQL Mail XPs' 的 过程'sys.xp_sendmail' 的访问的解决方法
    【Vegas原创】获取SQL Server处理语句的时间(毫秒)
  • 原文地址:https://www.cnblogs.com/FdWzy/p/14108342.html
Copyright © 2011-2022 走看看