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

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

问题描述

我终于得到了我的主要应用程式版本(Tap Play MMO - check out out ;-)),我现在正在扩展它。



做这个我需要一个有四个独立按钮的圆圈,这些按钮本质上将是季度。我得出的结论是,圆形图像将需要构造四个图像,每个季度一个,但由于矩形图像形状的必要性,我将最终与一些重叠,虽然重叠将是透明。



让这个工作的最佳方式是什么?我需要一些真正简单的东西,我看过这个
http://iphonedevelopment.blogspot.com/2010/03/irregularly-shaped-uibuttons.html



之前尚未成功获取工作。任何人都可以提供一些建议?



如果它有什么不同,我会部署到一个iOS 3.X框架(将4.2下线,当4.2来out for iPad)

解决方案

我看不到,为什么需要重叠。

只需创建4


$ b

>请参阅此优秀项目



它是通过在覆盖的

中合并像素的alpha值来工作的。

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

   - (UIColor *)colorAtPixel:(CGPoint)point {
//如果点在图像坐标之外则取消
if(!CGRectContainsPoint(CGRectMake(0.0f,0.0f,self.size .width,self.size.height),point)){
return nil;
}


//创建1x1像素字节数组和位图上下文以将像素绘制到。
//参考: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);

//将我们感兴趣的像素绘制到位图上下文中
CGContextTranslateCTM(context,-pointX,pointY-(CGFloat)height);
CGContextDrawImage(context,CGRectMake(0.0f,0.0f,(CGFloat)width,(CGFloat)height),cgImage);
CGContextRelease(context);

//将颜色值[0..255]转换为浮点数[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];
}


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.

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?

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)

解决方案

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

edit after comment

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