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

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

问题描述

我终于发布了我的主要应用程序(Tap Play MMO - 检查它;-)),我现在正在努力扩展它.

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

要做到这一点,我需要有一个圆圈,里面有四个单独的按钮,这些按钮基本上是四分之一.我得出的结论是,圆形图像需要由四张图像构成,每季度一张,但由于矩形图像形状的必要性,我最终会出现一些重叠,尽管重叠将是透明.

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-形状 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 用于 iPad 时将是 4.2)

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.

它的工作原理是在被覆盖的像素中加入 alpha 值

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天全站免登陆