采用不规则形状的按钮,简单的方法 [英] Simple way of using irregular shaped buttons

查看:147
本文介绍了采用不规则形状的按钮,简单的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我终于得到了我的主要的应用程序版本(点击播放MMO - 看看;-)),我现在正在扩大它

I've finally got my main app release (Tap Play MMO - check it out ;-) ) and I'm now working on expanding it.

要做到这一点,我需要有一个具有四扇独立按钮的圈子,这些按钮将主要是宿舍。我得出的结论是,circlular图像将需要建设四象,各一个季度,但由于矩形图像形状,我将用一些重叠结束的必要性,虽然重叠会透明的。

To do this I need to have a circle that has four seperate buttons in it, these buttons will essentially be quarters. I've come to the conclusion that the circlular image will need to be constructed of four images, one for each quarter, but due to the necessity of rectangular image shapes I'm going to end up with some overlap, although the overlap will be transparent.

什么是得到这个工作的最佳方式?我需要的东西很简单真的,我已经看了这
http://iphonedevelopment.blogspot.com/2010/03/irregularly-shaped-uibuttons.html

What's the best way of getting this to work? I need something really simple really, I've looked at this http://iphonedevelopment.blogspot.com/2010/03/irregularly-shaped-uibuttons.html

在,但没有成功地得到它的工作。任何人都可以提供一些建议吗?

Before but not yet succeeded in getting it to work. Anyone able to offer some advice?

在情况下,这有什么差别,我将部署到的iOS 3.X框架(将4.2向下行,当4.2出来为iPad)

In case it makes any difference I'll be deploying to a iOS 3.X framework (will be 4.2 down the line when 4.2 comes out for iPad)

推荐答案

我看不到,需要为什么重叠。结果
只需创建4个按钮,并给每一个你的形象片。

I can't see, why overlapping is needed.
Just create 4 buttons and give each one a slice of your image.

注释后修改

看到这个伟大工程。一个例子是正是你想做的事。

see this great project. One example is exactly what you want to do.

它通过掺入在被覆盖的像素的α值

It works by incorporating the alpha-value of a pixel in the overwritten

pointInside:withEvent:方法和UIImage的类别,添加此方法

pointInside:withEvent: and a category on UIImage, that adds this method

- (UIColor *)colorAtPixel:(CGPoint)point {
    // Cancel if point is outside image coordinates
    if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, self.size.width, self.size.height), point)) {
        return nil;
    }


    // Create a 1x1 pixel byte array and bitmap context to draw the pixel into.
    // Reference: http://stackoverflow.com/questions/1042830/retrieving-a-pixel-alpha-value-for-a-uiimage
    NSInteger pointX = trunc(point.x);
    NSInteger pointY = trunc(point.y);
    CGImageRef cgImage = self.CGImage;
    NSUInteger width = self.size.width;
    NSUInteger height = self.size.height;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    int bytesPerPixel = 4;
    int bytesPerRow = bytesPerPixel * 1;
    NSUInteger bitsPerComponent = 8;
    unsigned char pixelData[4] = { 0, 0, 0, 0 };
    CGContextRef context = CGBitmapContextCreate(pixelData, 
                                                 1,
                                                 1,
                                                 bitsPerComponent, 
                                                 bytesPerRow, 
                                                 colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);
    CGContextSetBlendMode(context, kCGBlendModeCopy);

    // Draw the pixel we are interested in onto the bitmap context
    CGContextTranslateCTM(context, -pointX, pointY-(CGFloat)height);
    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), cgImage);
    CGContextRelease(context);

    // Convert color values [0..255] to floats [0.0..1.0]
    CGFloat red   = (CGFloat)pixelData[0] / 255.0f;
    CGFloat green = (CGFloat)pixelData[1] / 255.0f;
    CGFloat blue  = (CGFloat)pixelData[2] / 255.0f;
    CGFloat alpha = (CGFloat)pixelData[3] / 255.0f;
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

这篇关于采用不规则形状的按钮,简单的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆