iOS隨機改變背景色及顯示數字

來源:互聯網
上載者:User

標籤:驗證碼   change   背景色   網站   

附上代碼,類似於註冊網站上的驗證碼

付圖:

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/71/81/wKioL1XSz7iRuETxAAA3hZbJwgo878.jpg" style="float:none;" title="螢幕快照 2015-08-18 下午2.13.31.png" alt="wKioL1XSz7iRuETxAAA3hZbJwgo878.jpg" />

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/71/84/wKiom1XSza3R2sPyAAA-To6Ogsk260.jpg" style="float:none;" title="螢幕快照 2015-08-18 下午2.13.38.png" alt="wKiom1XSza3R2sPyAAA-To6Ogsk260.jpg" />

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/71/81/wKioL1XSz7nBbRN5AABOBqoX_tE616.jpg" style="float:none;" title="螢幕快照 2015-08-18 下午2.13.48.png" alt="wKioL1XSz7nBbRN5AABOBqoX_tE616.jpg" />

- (void)change

{

    self.changeArray = [[NSArray alloc] initWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",@"n",@"o",@"p",@"q",@"r",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z",nil];

    

    NSMutableString *getStr = [[NSMutableString alloc] initWithCapacity:5];

    

    self.changeString = [[NSMutableString alloc] initWithCapacity:6];

    for(NSInteger i = 0; i < 4; i++)

    {

        NSInteger index = arc4random() % ([self.changeArray count] - 1);

        getStr = [self.changeArray objectAtIndex:index];

        

        self.changeString = (NSMutableString *)[self.changeString stringByAppendingString:getStr];

    }

}


- (void)drawRect:(CGRect)rect {

    [super drawRect:rect];

    

    float red = arc4random() % 100 / 100.0;

    float green = arc4random() % 100 / 100.0;

    float blue = arc4random() % 100 / 100.0;

    UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];

    [self setBackgroundColor:color];

    

    NSString *text = [NSString stringWithFormat:@"%@",self.changeString];

    CGSize cSize = [@"S" sizeWithFont:[UIFont systemFontOfSize:20]];

    int width = rect.size.width / text.length - cSize.width;

    int height = rect.size.height - cSize.height;

    CGPoint point;

    

    float pX, pY;

    for (int i = 0; i < text.length; i++)

    {

        NSLog(@"rect.size.width / text.length * i%f",rect.size.width / text.length * i);

        pX = arc4random() % width + rect.size.width / text.length * i;

        pY = arc4random() % height;

        point = CGPointMake(pX, pY);

        unichar c = [text characterAtIndex:i];

        NSString *textC = [NSString stringWithFormat:@"%C", c];

        [textC drawAtPoint:point withFont:[UIFont systemFontOfSize:20]];

    }

    

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(context, 1.0);

    

    for(int cout = 0; cout < 10; cout++) {

        red = arc4random() % 100 / 100.0;

        green = arc4random() % 100 / 100.0;

        blue = arc4random() % 100 / 100.0;

        color = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];

        CGContextSetStrokeColorWithColor(context, [color CGColor]);

        pX = arc4random() % (int)rect.size.width;

        pY = arc4random() % (int)rect.size.height;

        CGContextMoveToPoint(context, pX, pY);

        pX = arc4random() % (int)rect.size.width;

        pY = arc4random() % (int)rect.size.height;

        CGContextAddLineToPoint(context, pX, pY);

        CGContextStrokePath(context);

    }

}


iOS隨機改變背景色及顯示數字

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.