I recently updated to Xcode 7, beta 3.
And I've run into some issues, I can't seem to find any questions for on SO.
When I run my application, i get 3 errors:
Use of unresolved identifier 'kCGBlendModeMultiply'
Use of unresolved identifier 'kCGLineCapRound'
Use of unresolved identifier 'kCGLineJoinMiter'
However the 2 latter ones, disappear, although I assume they will show up after the first one is fixed (hence why I included it in this question).
I didn't see anything in the release notes about these being removed? So I'm a bit stuck at what to do. I tried rewriting the lines of course, but the 3 things I used don't show up as options anymore. In case that they are just gone in the latest Swift 2.0, what can I use instead?
Here's the code for the first error.
func alpha(value:CGFloat)->UIImage
{
UIGraphicsBeginImageContextWithOptions(self.size, false, 0.0)
let ctx = UIGraphicsGetCurrentContext()
let area = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -area.size.height)
CGContextSetBlendMode(ctx, kCGBlendModeMultiply)
CGContextSetAlpha(ctx, value)
CGContextDrawImage(ctx, area, self.CGImage)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage;
}
Here's the code for the 2 latter errors:
for layer in [ self.top, self.middle, self.bottom ] {
layer.fillColor = nil
layer.strokeColor = UIColor.whiteColor().CGColor
layer.lineWidth = 4
layer.miterLimit = 4
layer.lineCap = kCALineCapRound
layer.masksToBounds = true
let strokingPath = CGPathCreateCopyByStrokingPath(layer.path, nil, 4, kCGLineCapRound, kCGLineJoinMiter, 4)
layer.bounds = CGPathGetPathBoundingBox(strokingPath)
layer.actions = [
"strokeStart": NSNull(),
"strokeEnd": NSNull(),
"transform": NSNull()
]
self.layer.addSublayer(layer)
}
Any help would be greatly appreciated! :)
This should work:
CGContextSetBlendMode(ctx, CGBlendMode.Multiply)
... or even just this:
CGContextSetBlendMode(ctx, .Multiply)
If you Ctrl-click on CGContextSetBlendMode and then from its declaration jump (in the same way) to declaration of CGBlendMode then you will see:
enum CGBlendMode : Int32 {
/* Available in Mac OS X 10.4 & later. */
case Normal
case Multiply
case Screen
case Overlay
// ...
}
Similarly, the other line that produces the error should be changed to:
let strokingPath = CGPathCreateCopyByStrokingPath(layer.path, nil, 4, .Round, .Miter, 4)
Related
I don't know if anybody else has met this problem. I updated my Xcode to 8.2.1 today. When I tried to place several buttons on a scrollView, I found a type inference error. The core code is shown below:
for i in 0 ..< 6 {
let titleButton = UIButton(frame: CGRect(x: CGFloat(i) * titleWidth, y: 0, width: titleWidth, height: titleHeight))
sv.addSubview(titleButton)
}
The code had no problem to place 6 buttons on the scrollView(sv), but when I tried to set a title for each button Using call:setTitle(_ title: String?, for state: UIControlState) under the initialization of the button, Xcode did not show method completion suggest. I found the reason is 'titleButton' is inferred as error type by Xcode.
Then I tried:
for i in 0 ..< 6 {
let frame = CGRect(x: CGFloat(i) * titleWidth, y: 0, width: titleWidth, height: titleHeight)
let titleButton = UIButton(frame: frame)
sv.addSubview(titleButton)
}
And found 'frame' is inferred as error type, so the type inference failure of 'titleButton' seems from the frame. Finally I found that the variable 'i' from the beginning of this for statement is inferred as error type, that is the origin.
I didn't find similar problem in past Xcode versions. If I really need to use 'i' in my logic among for-loop, is there someone who has an idea to solve the inference error?
To fix this, I have to indicate the type myself as follow:
let titleButton : UIButton = UIButton(frame: frame)
Then under this, the method completion suggestion works fine. Hope Apple can fix the type inference error quickly...
let nameBox = UILabel(x: 0, y: 0, w: sideSize, h: sideSize*2/4)
nameBox.text = skillName
nameBox.textAlignment = .Center
nameBox.numberOfLines = 0
nameBox.adjustsFontSizeToFitWidth = true
nameBox.addBorderLeft(size: 1, color: UIColor.blackColor())
nameBox.addBorderTop(size: 1, color: UIColor.blackColor())
nameBox.addBorderRight(size: 1, color: UIColor.blackColor())
container.addSubview(nameBox)
This is the code I have and its output is below.
As you can see it has modified everything perfectly except for Communication & Lumberjack. Why is it, and how can I solve it?
Looks like you need to set the lineBreakMode of the label to .ByWordWrapping.
Another thought is that you really want the font to be smaller. Perhaps this is prevented by the minimumScaleFactor property.
From the docs for adjustsFontSizeToFitWidth:
The default value for this property is false. If you change it to true, you should also set an appropriate minimum font size by modifying the minimumFontSize property.
This seems to be a documentation bug. minimumFontSize is deprecated.
Im trying to get a sprite sheet to run through the 4 images so it plays an animation, although Corona SDK is giving me an error saying
"main.lua40: bad argument #2 to 'newImageSheet' (table (options)expected)
stack traceback: [C]: in function 'newImageSheet' main.lua 40: in main chunk"
exclamationMarkAnimations = graphics.newImageSheet("exclamationMarkAnimated.png", sheetOptions)
exclamationMarkAnimated =
{
width = 12,
height = 12,
numFrames = 4
}
animation = {
{
name = "bobbing",
start = 1,
count = 4,
time = 800,
loopCount = 0, -- "0" means always
loopDirection = "forward"
}
}
exclamationMarkVars = display.newSprite(exclamationMarkAnimations, exclamationMarkAnimated)
exclamationMarkVars:addEventListener("sprite", spriteListener)
Thank you for any and all help.
That project is saved under Corona samples as you can see in the picture below:
and also in this folder: C:\Program Files (x86)\Corona Labs\Corona SDK\Sample Code\Animation
You can use that as the base so save you some time.
I am trying to save an instance of NSColor to a file like this:
writeF(node.lineColour.hueComponent)
writeF(node.lineColour.saturationComponent)
writeF(node.lineColour.brightnessComponent)
writeF(node.lineColour.alphaComponent)
where the write function is:
func writeF(var val: CGFloat) -> Bool {
let nsd = NSData(bytes: &val, length: sizeof(CGFloat))
let rv = oStream!.write(UnsafePointer(nsd.bytes), maxLength: sizeof(CGFloat))
return rv > 0
}
And "node.lineColour" is just NSColor.blueColor(). It all compiles OK, but gives a run-time message at the first "writeF" line:
2015-10-01 07:57:43.871 canl[77917:8371660] An uncaught exception was raised
2015-10-01 07:57:43.871 canl[77917:8371660] *** -hueComponent not valid for the NSColor NSCalibratedWhiteColorSpace 0 1; need to first convert colorspace.
Apple documentation on color spaces is very esoteric (if you already understand it then it's a fine reference, but if not then... good luck). Why is the above code wrong? Should be able to at least retrieve the color components (CGFloat).
After swimming through the available documentation and trying different things, I found this to work:
let aColor = node.lineColor.colorUsingColorSpaceName(NSCalibratedRGBColorSpace)
if let culoare = aColor {
writeF(culoare.redComponent)
writeF(culoare.greenComponent)
writeF(culoare.blueComponent)
writeF(culoare.alphaComponent)
}
It also works for getting the hue, saturation, and brightness components, but I think I will go with RGB.
I know how to create TTButton:
TTButton *epriceButton = [TTButton buttonWithStyle:#"epriceButton:"];
And I have a method:
- (TTStyle *)epriceButton:(UIControlState)state {
TTShape* shape;
shape = [TTRoundedRectangleShape shapeWithRadius:4.5];
UIColor* tintColor = RGBCOLOR(8, 101, 191);
return [TTSTYLESHEET toolbarButtonForState:state shape:shape tintColor:tintColor font:nil];
}
That works nice. Last one thing I want from that button is to have its title left aligned. I'm new to three20 library, so I think I don't understand how styles work. I found that each TTStyle has next: method. But how multiple styles work together?
The text alignment for the button is defined inside -(TTStyle*)toolbarButtonForState: shape: tintColor: font:. So you will need to create your own version of this method.
If you look into the original you'll find a TTTextStyle as the last style being added. TTTextStyle let's you set the textAlignment with the convenient initializer
+ (TTTextStyle*)styleWithFont:(UIFont*)font color:(UIColor*)color
minimumFontSize:(CGFloat)minimumFontSize
shadowColor:(UIColor*)shadowColor shadowOffset:(CGSize)shadowOffset
textAlignment:(UITextAlignment)textAlignment
verticalAlignment:(UIControlContentVerticalAlignment)verticalAlignment
lineBreakMode:(UILineBreakMode)lineBreakMode numberOfLines:(NSInteger)numberOfLines
next:(TTStyle*)next
So your implementation might look like:
- (TTStyle*)myButtonForState:(UIControlState)state shape:(TTShape*)shape
tintColor:(UIColor*)tintColor font:(UIFont*)font {
UIColor* stateTintColor = [self toolbarButtonColorWithTintColor:tintColor forState:state];
UIColor* stateTextColor = [self toolbarButtonTextColorForState:state];
return
[TTShapeStyle styleWithShape:shape next:
[TTInsetStyle styleWithInset:UIEdgeInsetsMake(2, 0, 1, 0) next:
[TTShadowStyle styleWithColor:RGBACOLOR(255,255,255,0.18) blur:0 offset:CGSizeMake(0, 1) next:
[TTReflectiveFillStyle styleWithColor:stateTintColor next:
[TTBevelBorderStyle styleWithHighlight:[stateTintColor multiplyHue:1 saturation:0.9 value:0.7]
shadow:[stateTintColor multiplyHue:1 saturation:0.5 value:0.6]
width:1 lightSource:270 next:
[TTInsetStyle styleWithInset:UIEdgeInsetsMake(0, -1, 0, -1) next:
[TTBevelBorderStyle styleWithHighlight:nil shadow:RGBACOLOR(0,0,0,0.15)
width:1 lightSource:270 next:
[TTBoxStyle styleWithPadding:UIEdgeInsetsMake(8, 8, 8, 8) next:
[TTImageStyle styleWithImageURL:nil defaultImage:nil
contentMode:UIViewContentModeScaleToFill size:CGSizeZero next:
[TTTextStyle styleWithFont:font color:stateTextColor
minimumFontSize:14.0
shadowColor:[UIColor colorWithWhite:0 alpha:0.4] shadowOffset:CGSizeMake(0, -1)
textAlignment:UITextAlignmentLeft
verticalAlignment:UIControlContentVerticalAlignmentCenter
lineBreakMode:UILineBreakModeWordWrap numberOfLines:1
next:nil]]]]]]]]]];
}